使用来自 h2 数据库的数据通过 Junit 测试模拟获取请求
Mock get request with Junit tests using data from h2 database
我知道我们可以模拟获取请求。我们可以模拟 get 请求和来自 h2 数据库的 return 数据吗?
我的主要应用程序使用 Oracle 数据库。我能够填充 h2 数据库并编写 Junit 测试。但我刚刚调用了一个服务。我想发出一个获取请求并让它从 h2 数据库中提取数据。如何为此编写 Junit 测试?
感谢您的宝贵时间和帮助。
最简单的方法是 TestRestTemplate
。
使用 TestRestTemplate 和@SpringBootTest
进行测试
我认为这是测试您正在尝试做的事情的最简单方法之一。它会启动您的整个 Spring 引导应用程序,并让您能够通过 TestRestTemplate
与您的 REST Api 进行交互。
您需要使用一些 Maven 依赖项配置您的项目:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
然后,创建一个测试 class。此 class 将启动您的 Spring 引导应用程序并执行您的测试。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationTest {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
void testApplication() {
int resourceId = 3;
ResponseEntity<MyResponseObject> response = testRestTemplate.getForEntity("/myEndPoint/{resourceId}", MyResponseObject, resourceId);
assertThat(response.getStatusCode()).isEqualTo(200);
MyResponseObject body = response.getBody();
// then do what you want
}
}
基本上,就是这样。以下是需要考虑的事项:
@SpringBootTest
将查找由 @SpringBootApplication
或 @SpringBootConfiguration
注释的您。然后,它将加载您拥有的所有内容(包括服务和 h2 访问权限)。
SpringBootTest.WebEnvironment.RANDOM_PORT
将在您计算机上的任何可用端口上启动您的服务器。然后您可以同时拥有多个实例 运行。
- 通过将 h2 添加到测试环境中的 class 路径,Spring 将自动创建数据源以与您的 h2 数据库对话。不需要额外的配置(通常我的意思)。
TestRestTemplate
由Spring Boot 自动提供。正如您在我的测试中注意到的那样,我既不提供主机也不提供我的 SpringBoot 应用程序在测试中的端口。 Spring 自动连接到好的服务器...神奇。
如果您想要 tuto,请按照以下步骤操作:https://www.baeldung.com/spring-boot-testresttemplate。
如果你想要 Spring 引导测试文档,请按照以下步骤操作:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications
什么时候应该使用 TestRestTemplate
:
- 当您跨应用程序的所有层进行测试时
- 当您希望进行与生产环境非常接近的测试时
什么时候不应该:
- 当您的应用程序太长而无法启动时。当它加载有关您的应用程序的所有内容时,如果它通常需要 20 秒才能启动,那么您的测试将至少需要 20 秒才能执行。好长啊
- 当你对你根本无法控制的系统产生依赖时,你也必须使用
@MockBean
。这不是灵丹妙药。例如,它可以在您的本地计算机上运行,但不能在您的 CI 服务器上运行。小心点。
使用 MockMvc 进行测试
使用 MockMvc
您还可以模拟对控制器的测试,您可以模拟请求。这比全局 @SpringBootTest
更具限制性,因为您不会启动整个应用程序,而只是一小部分,用于测试 Web 控制器层。大多数时候,您不会使用它通过模拟其余部分来测试应用程序的所有层,仅测试 Web 部件。
总有办法通过所有层测试,但很明显,你需要添加class你想自己开火
如果你想看看它的样子,去看看这个:https://www.baeldung.com/integration-testing-in-spring
如果您有更多问题,请随时提问。
我知道我们可以模拟获取请求。我们可以模拟 get 请求和来自 h2 数据库的 return 数据吗?
我的主要应用程序使用 Oracle 数据库。我能够填充 h2 数据库并编写 Junit 测试。但我刚刚调用了一个服务。我想发出一个获取请求并让它从 h2 数据库中提取数据。如何为此编写 Junit 测试?
感谢您的宝贵时间和帮助。
最简单的方法是 TestRestTemplate
。
使用 TestRestTemplate 和@SpringBootTest
进行测试我认为这是测试您正在尝试做的事情的最简单方法之一。它会启动您的整个 Spring 引导应用程序,并让您能够通过 TestRestTemplate
与您的 REST Api 进行交互。
您需要使用一些 Maven 依赖项配置您的项目:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
然后,创建一个测试 class。此 class 将启动您的 Spring 引导应用程序并执行您的测试。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationTest {
@Autowired
private TestRestTemplate testRestTemplate;
@Test
void testApplication() {
int resourceId = 3;
ResponseEntity<MyResponseObject> response = testRestTemplate.getForEntity("/myEndPoint/{resourceId}", MyResponseObject, resourceId);
assertThat(response.getStatusCode()).isEqualTo(200);
MyResponseObject body = response.getBody();
// then do what you want
}
}
基本上,就是这样。以下是需要考虑的事项:
@SpringBootTest
将查找由@SpringBootApplication
或@SpringBootConfiguration
注释的您。然后,它将加载您拥有的所有内容(包括服务和 h2 访问权限)。SpringBootTest.WebEnvironment.RANDOM_PORT
将在您计算机上的任何可用端口上启动您的服务器。然后您可以同时拥有多个实例 运行。- 通过将 h2 添加到测试环境中的 class 路径,Spring 将自动创建数据源以与您的 h2 数据库对话。不需要额外的配置(通常我的意思)。
TestRestTemplate
由Spring Boot 自动提供。正如您在我的测试中注意到的那样,我既不提供主机也不提供我的 SpringBoot 应用程序在测试中的端口。 Spring 自动连接到好的服务器...神奇。
如果您想要 tuto,请按照以下步骤操作:https://www.baeldung.com/spring-boot-testresttemplate。
如果你想要 Spring 引导测试文档,请按照以下步骤操作:https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-testing-spring-boot-applications
什么时候应该使用 TestRestTemplate
:
- 当您跨应用程序的所有层进行测试时
- 当您希望进行与生产环境非常接近的测试时
什么时候不应该:
- 当您的应用程序太长而无法启动时。当它加载有关您的应用程序的所有内容时,如果它通常需要 20 秒才能启动,那么您的测试将至少需要 20 秒才能执行。好长啊
- 当你对你根本无法控制的系统产生依赖时,你也必须使用
@MockBean
。这不是灵丹妙药。例如,它可以在您的本地计算机上运行,但不能在您的 CI 服务器上运行。小心点。
使用 MockMvc 进行测试
使用 MockMvc
您还可以模拟对控制器的测试,您可以模拟请求。这比全局 @SpringBootTest
更具限制性,因为您不会启动整个应用程序,而只是一小部分,用于测试 Web 控制器层。大多数时候,您不会使用它通过模拟其余部分来测试应用程序的所有层,仅测试 Web 部件。
总有办法通过所有层测试,但很明显,你需要添加class你想自己开火
如果你想看看它的样子,去看看这个:https://www.baeldung.com/integration-testing-in-spring
如果您有更多问题,请随时提问。