我正在尝试使用DBUnit来运行集成测试,但是我发现自己无法插入主键列,这显然不适用于稍后在文件中引用主键的外键。
例如,我有以下DDL:
CREATE TABLE attributes(
attribute_id UUID NOT NULL DEFAULT uuid_generate_v4(),
attribute VARCHAR(64) NOT NULL,
description TEXT NOT NULL,
PRIMARY KEY(attribute_id)
);
DBUnit设置XML看起来像:
<?xml version="1.0" encoding="UTF-8" ?>
<dataset>
<attributes attribute_id="233bc966-4fcd-4b46-88e6-3e07090f322d" attribute="Empathy" description="Empathy Description" />
</dataset>
当我尝试运行测试时,我得到了失败:
org.dbunit.dataset.NoSuchColumnException: attributes.ATTRIBUTE_ID - (Non-uppercase input column: attribute_id) in ColumnNameToIndexes cache map. Note that the
ap's column names are NOT case sensitive.
以下是正在运行的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@DbUnitConfiguration(dataSetLoader = TestConfiguration.FlatXmlDataLoaderProxy.class)
@ContextConfiguration(classes = {ApplicationConfiguration.class, TestConfiguration.class})
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class
})
public class ApplicationAssessmentJobTest {
@Autowired
private ApplicationAssessmentJob applicationAssessmentJob;
@Test
@DatabaseSetup("/dbunit/ApplicationAssessmentJobTestSetup.xml")
@DatabaseTearDown("dbunit/ApplicationAssessmentJobTestTearDown.xml")
public void testJob() {
ApplicationAssessmentJobModel model = new ApplicationAssessmentJobModel();
model.setApplicationId(UUID.fromString("41fa1d51-c1ee-482b-80a7-a6eefda64436"));
applicationAssessmentJob.receiveMessage(model);
}
}
显示的此错误似乎与潜在问题没有直接关系。如果我从XML中删除attribute_id
列,则会插入记录。
答案 0 :(得分:4)
我遇到了同样的问题,我终于找到了解决方案。 如文档中所提到的,您需要覆盖默认的dbunit DatabaseConfig以设置特定于PostgreSQL的IDataTypeFactory。
这是我的测试配置:
@Autowired
private DataSource dataSource;
@Bean
public DatabaseConfigBean databaseConfigBean() {
DatabaseConfigBean databaseConfigBean = new DatabaseConfigBean();
databaseConfigBean.setDatatypeFactory(new PostgresqlDataTypeFactory());
return databaseConfigBean;
}
@Bean(name = "dbUnitDatabaseConnection")
public DatabaseDataSourceConnectionFactoryBean dbUnitDatabaseConnection() throws SQLException, DatabaseUnitException, IOException {
DatabaseDataSourceConnectionFactoryBean databaseDataSourceConnectionFactoryBean = new DatabaseDataSourceConnectionFactoryBean();
databaseDataSourceConnectionFactoryBean.setDatabaseConfig(databaseConfigBean());
databaseDataSourceConnectionFactoryBean.setDataSource(dataSource);
databaseDataSourceConnectionFactoryBean.setSchema("public");
return databaseDataSourceConnectionFactoryBean;
}
然后,您需要指定在测试中使用自定义IDatabaseConnection。这些连接将由bean&lt; dbUnitDatabaseConnection&#39;创建。例如,这是我如何声明我的弹簧测试配置:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringRepositoryConfigurationTest.class})
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DbUnitTestExecutionListener.class })
@DbUnitConfiguration(databaseConnection = {"dbUnitDatabaseConnection"})
答案 1 :(得分:2)
在我的大部分时间里与DBUnit / Spring-Test-DBUnit战斗之后,我决定废弃该库并自行推出,因为DBUnit似乎只是痛苦而已。
我能够在不到30分钟的时间内在大约40 SLoC中编写设置/拆卸。它使用普通的SQL,这在哲学上更符合我对Hibernate jOOQ的选择。当然不太理想,但15分钟的搜索并没有找到任何东西来处理在安装和拆卸时运行SQL的简单用例。值得注意的是java.sql.DataSource
提供ApplicationContext
Bean的要求。
DbInitTestExecutionListener.java
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestExecutionListener;
import javax.sql.DataSource;
import java.util.Arrays;
public class DbInitTestExecutionListener implements TestExecutionListener {
@Override
public void beforeTestClass(TestContext testContext) throws Exception {}
@Override
public void prepareTestInstance(TestContext testContext) throws Exception {}
@Override
public void beforeTestMethod(TestContext testContext) throws Exception {
DatabaseSetup setup = testContext.getTestMethod().getAnnotation(DatabaseSetup.class);
if (setup != null) {
if (setup.clearInsert()) {
afterTestMethod(testContext);
}
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
Arrays.asList(setup.value()).stream()
.map(ClassPathResource::new)
.forEach(populator::addScript);
populator.execute(testContext.getApplicationContext().getBean(DataSource.class));
}
}
@Override
public void afterTestMethod(TestContext testContext) throws Exception {
DatabaseTearDown tearDown = testContext.getTestMethod().getAnnotation(DatabaseTearDown.class);
if (tearDown != null) {
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
Arrays.asList(tearDown.value()).stream()
.map(ClassPathResource::new)
.forEach(populator::addScript);
populator.execute(testContext.getApplicationContext().getBean(DataSource.class));
}
}
@Override
public void afterTestClass(TestContext testContext) throws Exception {}
}
DatabaseTearDown.java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface DatabaseTearDown {
String[] value();
}
DatabaseSetup.java
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD })
public @interface DatabaseSetup {
boolean clearInsert() default true;
String[] value();
}
使用最少的测试配置:
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = {ApplicationConfiguration.class})
@TestExecutionListeners({
DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbInitTestExecutionListener.class
})
public class TestClass {
@Test
@DatabaseSetup("/dbinit/TestSetup.sql")
@DatabaseTearDown("/dbinit/TestTearDown.sql")
public void testJob() {
// TODO: Add test code here
}
}
答案 2 :(得分:0)
此错误消息
org.dbunit.dataset.NoSuchColumnException: attributes.ATTRIBUTE_ID ...
建议dbunit正在寻找名为ATTRIBUTE_ID
的列。您的CREATE TABLE语句创建一个名为attribute_id
的列。
如果dbunit使用分隔标识符,PostgreSQL会将"ATTRIBUTE_ID"
和"attribute_id"
视为两个不同的标识符。 (在您的情况下,两个不同的列。)如果dbunit 没有使用分隔标识符,PostgreSQL将折叠为小写,它将考虑ATTRIBUTE_ID
和attribute_id
是相同的标识符 - attribute_id
。
SQL标准要求将普通标识符折叠为大写。 PostgreSQL的行为不是标准的,但它不太可能改变。
看起来就像dbunit本身可以折叠成大写(遵循SQL标准)。如果是这种情况,您可以将dbunit配置为使用区分大小写的标识符。 (我不知道如何做到这一点。)