springBoot JunitTest的使用
@SpringBootTest(classes = {Application.class},webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestExecutionListeners(listeners = {TestListener.class, DependencyInjectionTestExecutionListener.class })
@TestPropertySource(locations = {"file:../config/prop.properties"})
public class Test {
@Autowired
private Service Service;
@Test
public void test(){
Service.testMethod("11111111");
}
}
首先是@TestExecutionListeners 注解,这个注解可以在junit测试开始之前进行设置环境变量或做其他的事情
@TestPropertySource 这个注解可以为environment加载一些配置文件,这些配置文件必须是properties,使用过程中发现yaml解析不了。