我正在运行一些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带来了一个空列表。