Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Answer

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...