【sprinb-boot】Junit测试
生活随笔
收集整理的這篇文章主要介紹了
【sprinb-boot】Junit测试
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
- springboot 2.0.0.RELEASE
- maven 3.5.0
- 參考:https://docs.spring.io/spring-boot/docs/2.0.0.RELEASE/reference/htmlsingle/#boot-features-testing
引入dependency
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>Junit測試類:簡寫
Junit測試類
@RunWith(SpringRunner.class) @SpringBootTest public class TestProperties4Debug {private Logger log = LoggerFactory.getLogger(this.getClass());@Value("${debug}")private boolean isDebug;@Testpublic void t1() throws Exception {this.log.debug("debug : {}", this.isDebug);System.out.println(this.isDebug);} }- SpringBootTest 默認尋找 @SpringBootConfiguration。尋找順序:src/test/java > src/main/java。
- @SpringBootApplication 包含 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan)
- @SpringBootConfiguration 包含 @Configuration
- SpringBootTest 默認尋找 application.properties。尋找順序:src/test/resources > src/main/resources。
Junit測試類:@ContextConfiguration、@TestPropertySource
Junit測試類
@RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration(classes=Application.class) @TestPropertySource("classpath:application.properties") public class TestProperties4Debug2 {@Value("${debug}")private boolean isDebug;@Testpublic void t1() throws Exception {Assert.assertTrue(this.isDebug);}}Application類
@SpringBootApplication public class Application {public static void main(String[] args) throws IOException {SpringApplication.run(Application.class, args);}@Beanpublic CommandLineRunner commandLineRunner(ApplicationContext ctx) {return args -> {System.out.println("Application startup done.");};} }application.properties
debug=true總結
以上是生活随笔為你收集整理的【sprinb-boot】Junit测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IOS下,javascript 自带的
- 下一篇: 心肌梗塞的前兆和表现