mercredi 19 juin 2019

JUnit tests run separately but not all together using H2 database

I am running some JUnit tests that run perfectly separated one by one, but do not pass when they are all executed together. Here I show you some examples of the tests.

I tried to close the connection after each one of the tests, and to truncate the tables after each test, and the problem persisted.

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = DBInstallerApplication.class)
public class ContactDataDeleteTriggerTest {

  @Autowired
  DataSource dataSource;

  /**
   * Tests if the delete trigger works.
   */
  @Test
  @Transactional
  public void testContactDataDeleteTrigger_deleteContactData_entryWasWrittenInHisTable()
      throws SQLException {

     long contactDataId =
        ContactDataInsertTestDataUtil.insertContactDataForTest(dataSource.getConnection());

    String sql = "DELETE FROM CONTACT_DATA WHERE ID = ?";

    JdbcTemplate jdbcTemplateDelete = new JdbcTemplate(dataSource);
    int result = jdbcTemplateDelete.update(sql, new Object[]{contactDataId});
    assertTrue(result > 0, "Expected some result");

    sql = "SELECT * FROM HIS_CONTACT_DATA WHERE ID = ? AND OPERATION = 'DELETE'";

    JdbcTemplate jdbcTemplateSelect = new JdbcTemplate(dataSource);
    List<Object> resultSet =
        jdbcTemplateSelect.query(
            sql,
            new Object[]{contactDataId},
            (rs, rowNum) -> {
              return null;
            });
    assertFalse(resultSet.isEmpty());
    assertTrue(resultSet.size() == 1);

  }
}

Here i show you another one of the tests:

@ExtendWith(SpringExtension.class)
@SpringBootTest(classes = DBInstallerApplication.class)
public class ContactDataInsertTriggerTest {

  @Autowired
  DataSource dataSource;

  /**
   * Tests if the insert trigger works.
   */
  @Test
  @Transactional
  public void testContactDataInsertTrigger_insertContactData_entryWasWrittenInHisTable()
      throws SQLException {
    long contactDataId =
        ContactDataInsertTestDataUtil.insertContactDataForTest(dataSource.getConnection());

    String sql = "SELECT * FROM HIS_CONTACT_DATA WHERE ID = ? AND OPERATION = 'INSERT'";

    JdbcTemplate jdbcTemplateSelect = new JdbcTemplate(dataSource);
    List<Object> resultSet =
        jdbcTemplateSelect.query(
            sql,
            new Object[]{contactDataId},
            (rs, rowNum) -> {
              return null;
            });
    assertFalse(resultSet.isEmpty());
    assertTrue(resultSet.size() == 1);

  }
}

The problem that I have is, that on the second test (testContactDataInsertTrigger) the ResultSet brings an empty list.

Aucun commentaire:

Enregistrer un commentaire