Bla*_*gle 5 java integration-testing spring-boot spring-boot-test
我有一个 Spring Boot 集成测试和一个 Spring Boot Web 集成测试。两个测试在单独运行时都通过。但是,当作为套件的一部分运行时,第二个执行的测试总是失败。每个测试都会启动(并拆除)我的应用程序,进而启动我的h2数据库。我已经尝试过交换测试的顺序,但总是后一个测试失败。
我能做些什么来确保这些测试是独立的/不会相互影响?
注意:我正在使用类注释@RunWith(SpringRunner.class)和@SpringBootTest两个测试,Web 集成测试将参数传递webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT给后一个注释。
集成测试:
@Test
public void testFindAll() {
List<Object> objects = objectRepository.findAll();
assertThat(objects.size(), is(greaterThanOrEqualTo(0)));
}
Run Code Online (Sandbox Code Playgroud)
网络集成测试:
@Test
public void testListAll() throws IOException {
TestRestTemplate testRestTemplate = new TestRestTemplate();
ResponseEntity<String> responseEntity = testRestTemplate.getForEntity("url/api/v1/objects", String.class);
assertThat(responseEntity.getStatusCode(), equalTo(OK));
ObjectMapper objectMapper = new ObjectMapper();
JsonNode responseJson = objectMapper.readTree(responseEntity.getBody());
assertThat(responseJson.isMissingNode(), is(false));
assertThat(responseJson.toString(), equalTo("[]"));
}
Run Code Online (Sandbox Code Playgroud)
Mar*_*cel 10
我遇到了类似的问题,尽管我的测试涉及嵌入式 H2 DB 的更改。我通过在我的类上注释@DirtiesContextThis will have Spring reload ApplicationContext after your test 来解决这个问题。
| 归档时间: |
|
| 查看次数: |
2476 次 |
| 最近记录: |