java - Spring Boot 应用程序的测试如何进行

标签 java spring spring-boot integration-testing

我正在尝试了解 Spring Boot 及其所有工作。目前我正在努力进行集成测试,所以我去看了 spring-boot-samples 。我开始看spring-boot-sample-testng

在此集成测试示例中,我没有看到任何对 SampleTestNGApplication 类(主要方法所在)的引用。

@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 类中。

最佳答案

答案在 SpringBootTest doc 。我应该注意的

Automatically searches for a @SpringBootConfiguration when nested @Configuration is not used, and no explicit classes are specified.

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

关于java - Spring Boot 应用程序的测试如何进行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47979344/

相关文章:

java - Spring 数据休息: Return One-To-Many-To-One as Many-To-Many relation

java - 使用 JavaMail API 通过 servlet 发送的电子邮件无法通过

java - SpringMVC BeanCreationException : creating bean with name 'personService' defined in file PersonServiceImpl. 类?

java - 如何制作一个在系统的某个时间启动的方法?

java - @Autowired 注解在 Spring 中由 BeanPostProcessor 处理吗?

java - 有没有办法在 FreeMarker 电子邮件模板中包含 CSS?

java - Spring Boot - RestController 反序列化 YAML 上传?

java - 连续的 appendReplacement 调用失败

java - 获取数据时发生方向更改时的 RxJava2 UndeliverableException

java - GAE : How to map object oriented designs into Appengine datastore efficiently