Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

junit - Why test in spring boot application still works without @RunWith(SpringRunner.class) just with @SpringBootTest

I dont understand why my executing test nicely working without @RunWith(SpringRunner.class).I have only @SpringBootTest above the test class . Please explain next cases : 1)When I should necessarily set @RunWith(SpringRunner.class)and @SpringBootTest together ? 2)When I can just set only @RunWith(SpringRunner.class) and @SpringBootTest usage is redundant ? 3)When I can just set only @SpringBootTest and @RunWith(SpringRunner.class) usage is redundant ?

My code with nicely executing test is :

@SpringBootTest
//@RunWith(SpringRunner.class)
class SpringBootSimpleTestExampleApplicationTests {


    @Autowired
    private ApplicationContext applicationContext;


    @Test
    void contextLoads() {
        Object restTempalte = applicationContext.getBeanDefinitionCount();
        Assertions.assertThat(applicationContext).isNotNull();
    }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@SpringBootTest

It provides the following features over and above the regular Spring TestContext provided by @ContextConfiguration(classes=…?) annotation in spring-test.

Automatically searches for a @SpringBootConfiguration when nested @Configuration class is not used, and no explicit classes are specified. Allows custom environment properties to be defined using the properties attribute. Provides support for different webEnvironment modes, including the ability to start a fully running web server listening on a defined or random port. Registers a TestRestTemplate and/or WebTestClient bean for use in web tests that are using a fully running web server.

https://howtodoinjava.com/spring-boot2/testing/springboottest-annotation/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...