注解以及作用:
压制警告
@SuppressWarnings("deprecation")
表示使用Spring Test组件进行单元测试;
@RunWith(SpringJUnit4ClassRunner.class)
保证每次测试类执行完后数据库进行回滚,防止测试时产生脏数据
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true
)
@Transactional
指定Bean的配置文件信息,可以有多种方式,这个例子使用的是文件路径形式,如果有多个配置文件,可以将括号中的信息配置为一个字符串数组来表示;
如果spring的配置文件在默认的目录下
@ContextConfiguration(locations = "classpath:spring-config.xml")
如果spring的配置文件放在WEB-INF目录下,需要使用这种方法
@ContextConfiguration(locations={"file:src/main/webapp/WEB-INF/spring-config.xml"})
指定使用哪一个配置文件:dev int uat pro
@ActiveProfiles("dev")
@SpringApplicationConfiguration(classes=ServiceApplication.class)
使用这个Annotate会在跑单元测试的时候真实的启一个web服务,然后开始调用Controller的Rest API,
待单元测试跑完之后再将web服务停掉;
@WebAppConfiguration
我结合控制台打印信息查看后发现,这里的配置信息其实都能打印出来的,如下面:
下图是我自行翻译的,其实是汉式翻译,有大神看到希望可以指导下!
2017-3-21 09:41:22 main INFO Began transaction (1) for test context
[
DefaultTestContext@8cd36b 默认测试内容@:
testClass = PersonControllerTest, 测试类: PersonControllerTest
testInstance = com.smile.userinfo.success.PersonControllerTest@18d9199, 测试实例:PersonControllerTest@18d9199@
testMethod = updatePersonInfoTest@PersonControllerTest, 测试方法: updatePersonInfoTest@PersonControllerTest
testException = [null], 测试异常:null
mergedContextConfiguration = 配置上下文信息:
[
WebMergedContextConfiguration@1fa56fa 配置上下文信息实例@
testClass = PersonControllerTest, 测试类: PersonControllerTest
locations = '{}', 位置信息:
classes = '{class com.smile.ServiceApplication}', 启动类:ServiceApplication
contextInitializerClasses = '[]', 上下文初始化程序:
activeProfiles = '{}', 活动的配置文件:
propertySourceLocations = '{}', 本地资源属性:
propertySourceProperties = '{}', 配置文件资源属性:
contextCustomizers = set 上下文定制器:
[
org.springframework.test.context.web.socket.MockServerContainerContextCustomizer@12cdcf4, Mock org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@169c6ba, 过滤器:执行上下问配置过滤器 org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0 mock配置器
],
resourceBasePath = 'src/main/webapp', 基本资源地址:src/main/webapp
contextLoader = 'org.springframework.boot.test.SpringApplicationContextLoader', 上下文加载器
parent = [null] 父节点:null
]
];