在Spring Boot项目中test文件夹有什么作用?

时间:2019-02-07 14:38:53

标签: java spring unit-testing spring-boot annotations

我刚刚创建了spring boot项目,并且src中有一个名为test的文件夹。它是做什么的?

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class GprocApplicationTests {

@Test
public void contextLoads() {
}

}

在创建spring boot项目时,项目的src文件夹中存在test文件夹。在测试文件夹中,有一个用@RunWith(SpringRunner.class)@SpringBootTest注释的类。它是做什么的?

2 个答案:

答案 0 :(得分:3)

这是项目生成器添加的基本测试。

主要目的是为您设置测试基础结构(目录,所需的库等)。

此特定测试可确保应用程序启动时没有错误(因为您的应用程序尚未执行其他任何操作)。目的是让您在主文件夹中添加功能时利用此设置来添加自己的测试。

答案 1 :(得分:1)

Spring Boot遵循convention over configuration方法,因此它使用通用Java项目结构,这在构建系统mavengradle中是常见的。

因此,测试文件夹包含src/test/java下的测试源文件和src/test/resources下的测试资源。作为一种好的做法,Spring Boot包含测试类,该类可验证您的应用程序上下文是否正确启动。