将Spring Boot 2.0.7迁移到2.0.9时JUnit测试失败

时间:2019-06-17 08:06:52

标签: java spring-boot junit migration jdbctemplate

将项目从Spring Boot 2.0.7迁移到SpringBoot 2.0.9之后,JUnit不再起作用。 出现的错误是以下错误,它似乎与用于测试的H2数据库有关(错误消息:Table "HIS_CONTACT_DATA" not found):

2019-06-17 09:53:16.785  INFO 7056 --- [           main] o.s.t.c.transaction.TransactionContext   : Rolled back transaction for test: [DefaultTestContext@617c1ac2 testClass = ContactDataDeleteTriggerTest, testInstance = com.arvato.rdu.trigger.contactdata.ContactDataDeleteTriggerTest@566bea82, testMethod = testContactDataDeleteTrigger_deleteContactData_entryWasWrittenInHisTable@ContactDataDeleteTriggerTest, testException = org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [SELECT * FROM HIS_CONTACT_DATA WHERE ID = ? AND OPERATION = 'DELETE']; nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Tabla "HIS_CONTACT_DATA" no encontrada
Table "HIS_CONTACT_DATA" not found; SQL statement:
SELECT * FROM HIS_CONTACT_DATA WHERE ID = ? AND OPERATION = 'DELETE' [42102-199], mergedContextConfiguration = [WebMergedContextConfiguration@4b28d787 testClass = ContactDataDeleteTriggerTest, locations = '{}', classes = '{class com.arvato.rdu.installer.DBInstallerApplication, class com.arvato.rdu.installer.DBInstallerApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@124c278f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@1fe20588, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@5276e6b0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@96def03], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true]]
2019-06-17 09:53:16.795  INFO 7056 --- [       Thread-7] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@198c97d0: startup date [Mon Jun 17 09:53:12 CEST 2019]; root of context hierarchy
2019-06-17 09:53:16.795  INFO 7056 --- [       Thread-4] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@119020fb: startup date [Mon Jun 17 09:53:01 CEST 2019]; root of context hierarchy
2019-06-17 09:53:16.803  INFO 7056 --- [       Thread-4] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

这是与此错误相关的测试代码:

@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 =
        jdbcTemplateDelete.query(
            sql,
            new Object[]{contactDataId},
            (rs, rowNum) -> {
              return null;
            });
    assertFalse(resultSet.isEmpty());
    assertTrue(resultSet.size() == 1);
  }
}

0 个答案:

没有答案