做测试的时候在类上加 @Transactional纾解是为了防止对数据进行污染,使用这个注解完成功能测试又不影响原有数据,对于测试库和开发库没有分开的情况下是一个很不错的选择.如以下测试代码:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProductCategoryRepositoryTest {
 @Autowired
 private ProductCategoryRepository pcr;

 @Test
 @Transactional   //在测试中加入   @javax.transaction.Transactional , 可以在插入完数据后,完成测试,再回滚,这样不会对数据库造成影响。
 public void testSaveOneTest2(){
     ProductCategory p=new ProductCategory();
     p.setCategoryName("女生最爱");
     p.setCategoryType(3);
     p.setCreateTime(new Date() );
     p.setUpdateTime(new Date() );
     ProductCategory result = pcr.save(p);
     Assert.assertNotNull(result);
 }
}

测试完成后,数据库中的数据回滚,没有对数据库造成影响.
我们可以观察测试的日志,可以看到里面有回滚操作.

2021-04-16 16:37:36,346 - Began transaction (1) for test context [DefaultTestContext@757277dc testClass = ProductCategoryRepositoryTest, testInstance = com.yc.webchatres.repository.ProductCategoryRepositoryTest@6d420cdd, testMethod = testSaveOneTest2@ProductCategoryRepositoryTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@687e99d8 testClass = ProductCategoryRepositoryTest, locations = '{}', classes = '{class com.yc.webchatres.WebchatresApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@eb21112, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@706a04ae, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@7a36aefa, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6dde5c8c, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5fcd892a, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5a8806ef], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@4e50ae56]; rollback [true]
Hibernate: insert into product_category (category_name, category_type, create_time, update_time) values (?, ?, ?, ?)
2021-04-16 16:37:36,778 - Rolled back transaction for test: [DefaultTestContext@757277dc testClass = ProductCategoryRepositoryTest, testInstance = com.yc.webchatres.repository.ProductCategoryRepositoryTest@6d420cdd, testMethod = testSaveOneTest2@ProductCategoryRepositoryTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@687e99d8 testClass = ProductCategoryRepositoryTest, locations = '{}', classes = '{class com.yc.webchatres.WebchatresApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@eb21112, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@706a04ae, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@7a36aefa, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@6dde5c8c, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@5fcd892a, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5a8806ef], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]

这表明在junit中的@Transactional与在业务层中的@Transactional是不同的,在这里,不管是否有异常都会回滚.

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐