首页 文章

测试如何适用于Spring启动应用程序

提问于
浏览
0

我正在努力进行集成测试,所以我去了spring-boot-samples . 我开始看spring-boot-sample-testng

在这个集成测试示例中,我没有看到任何对SampleTestNGApplication类的引用,其中main方法是 .

@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class SampleTestNGApplicationTests extends AbstractTestNGSpringContextTests {

    @Autowired
    private TestRestTemplate restTemplate;

    @Test
    public void testHome() {
        ResponseEntity<String> entity = this.restTemplate.getForEntity("/", String.class);
        assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
        assertThat(entity.getBody()).isEqualTo("Hello World");
    }

}

它是如何知道main方法在SampleTestNGApplication类中的 .

2 回答

  • 0

    你正在调用 "/" 调用SampleController,它有一个方法:

    @GetMapping("/")
    

    HelloWorldService 返回"Hello World"

  • 0

    答案是SpringBootTest doc . 我应该注意到

    在未使用嵌套的@Configuration时自动搜索@SpringBootConfiguration,并且未指定显式类 .

    我的主类使用 @SpringBootApplication 进行注释,这是一个配置注释 .

相关问题