JUnit测试使用H2数据库单独运行,但不能一起运行

时间:2019-06-19 08:05:23

标签: database testing junit h2

我正在运行一些JUnit测试,它们完美地一一分开运行,但是当它们全部一起执行时不会通过。在这里,我向您展示一些测试示例。

我尝试在每个测试之后关闭连接,并在每次测试之后截断表,问题仍然存在。

@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);

  }
}

在这里,我向您展示了另一项测试:

@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);

  }
}

我遇到的问题是,在第二个测试(testContactDataInsertTrigger)上,ResultSet带来了一个空列表。

0 个答案:

没有答案