title: Integration tests work separately but not run together in Spring Boot app
body: <p>Seems like I am missing something essential here, but I have not managed to find it on my own.</p>
  
  <p>I have a Spring Boot app together with Hibernate that I am trying to test with some integration tests. I am using a HSQL-database while testing. The tests I am trying to run use MockMvc to check the json-responses my controllers are returning.</p>
  
  <p>I let my test classes extend a config class which uses @Before and @After to init test data and take it down afterwards.</p>
  
  <p>So, in my single test class I have four tests. Just doing simple, basic things like returning lists and single objects. They are all just reading data.</p>
  
  <pre><code>@Test
  public void testGetSingleData() throws Exception {
      this.mockMvc.perform(get("/api/data/{id}", 1)
              .andExpect(status().isOk());
  }
  @Test
  public void testGetListData() throws Exception {
      this.mockMvc.perform(get("/api/data/", 1)
              .andExpect(status().isOk());
  }
  …
  and so on.
  </code></pre>
  
  <p>What happens to be the case is that when I run these tests separately, that is one at a time, they all work fine. I get the expected output. However, when running the four tests together, one of them is not returning the same response. In fact, it is returning nothing. More specifically the testGetSingleData test returns an object when running alone. When running together with the other tests it returns nothing. My controller says the data is not in the database.
      It seems to me something is happening with the database in between the tests. Of course the data is initialized and taken down, but that should be okay. For example running the two tests that return a list of objects together works fine, but not together with the test that returns a single object. So the data is somehow gone when testGetSingleData is running.
  I guess I need a potential reason for why this is happening. What can be sources of error to a test working alone not functioning while running together with other tests?</p>
  
  <p>My class «test-config-class» is annotated with:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @WebAppConfiguration
  @ContextConfiguration(loader = SpringockitoAnnotatedWebContextLoader.class, classes = Application.class)
  @TestPropertySource(locations = "classpath:config/application.properties")
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <pre><code>@Autowired
  private EntityManagerFactory firstDbEntityManager;  
  @Autowired
  private EntityManagerFactory secondDbEntityManager;
  protected SessionFactory firstDbSessionFactory;
  protected SessionFactory secondDbSessionFactory;
  
  @Before
  public void initSessionFactories() {
      if (firstDbSessionFactory == null) {
          firstDbSessionFactory = firstDbEntityManager.unwrap(SessionFactory.class);
      }
      if (secondDbSessionFactory == null) {
          secondDbSessionFactory = secondDbEntityManager.unwrap(SessionFactory.class);
      }
  
      initTestData();
  
  }
  
  
  @After
  public void cleanDatabase() {
      doCleanDatabase(firstDbEntityManager);
      doCleanDatabase(secondDbEntityManager);
  }
  
  private void doCleanDatabase(EntityManagerFactory entityManagerFactory) {
          Session session = entityManagerFactory.unwrap(SessionFactory.class).openSession();
          session.createSQLQuery("truncate schema public and commit").executeUpdate();
          session.flush();
          session.close();
  }
  </code></pre>