7

I have a Spring Boot integration test and a Spring Boot web integration test. Both tests pass when run individually. However, when run as part of a suite, the test that is executed second always fails. Each test starts up (and tears down) my application and, in turn, my h2 database. I have experimented with swapping the order of the tests and it always the latter test that fails.

What can I do to ensure that these tests are self-contained/will not affect each other?

Note: I am using the class annotations @RunWith(SpringRunner.class) and @SpringBootTest for both tests, with the web integration test passing the argument webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT to the latter annotation.

Integration test:

@Test
public void testFindAll() {
    List<Object> objects = objectRepository.findAll();
    assertThat(objects.size(), is(greaterThanOrEqualTo(0)));
}

Web integration test:

@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("[]"));
}
Blair Nangle
  • 1,221
  • 12
  • 18
  • what version of spring boot are you using? https://github.com/spring-projects/spring-boot/issues/9282 – Pär Nilsson Jun 12 '17 at 18:58
  • I am using 1.5.3.RELEASE. However, I updated to 1.4.7.RELEASE and was still seeing the same issue, so I switched back to 1.5. I don't believe the issue you linked is responsible for the problem I'm having. If I annotate my tests with `@DirtiesContext` (to force a fresh application context to be reloaded for the affected tests), then they will pass – although, obviously this will increase overall suite execution time. My problem lies, I believe, in the state my tests are leaving the `ApplicationContext` in after executing. – Blair Nangle Jun 13 '17 at 08:46
  • can you post some other tests? – Pär Nilsson Jun 13 '17 at 09:21
  • Aside from a few unit tests, these are the only tests that I have in my project. – Blair Nangle Jun 13 '17 at 09:40
  • If you post at least one more test that will fail if run in sequence with the one you already posted I think it will be easier to get some idea of what the issue is. – Pär Nilsson Jun 13 '17 at 10:17
  • These two tests fail when run in series with each other, even if it is just the two of them running. I believe the problem lies in these tests or my wider Spring configuration. – Blair Nangle Jun 13 '17 at 10:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146536/discussion-between-blairnangle-and-par-nilsson). – Blair Nangle Jun 13 '17 at 13:25
  • Hey @blairnangle , did You manage to solve that problem? I have similar issue, and I'm kinda out of ideas. – patrykos91 May 11 '18 at 07:33
  • Hey @patrykos91, unfortunately not. I am also not using Spring Boot, currently, so would be a bit rusty on the technology. Sorry that I cannot be of more help. – Blair Nangle May 11 '18 at 10:31
  • I have the same problem. However, when I add a Thread.sleep(2000) at the beginning of the 2nd test, both run fine. That's why I think Spring is not finished with reseting the context or something in between tests. – woezelmann Nov 14 '18 at 16:39

1 Answers1

10

I had a similar problem, though my tests involve changes on an embedded H2 DB. I solved it by annotating my classes with @DirtiesContext This will have Spring reload ApplicationContext after your test.

Marcel
  • 568
  • 7
  • 12