我试图在我们的项目中使用DBUnit并遇到了问题。想知道你是否能够帮助它。
我正在使用@ExpectedDatabase(“file.xml”)注释,在处理时,Im在此执行点:
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:295)
at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:314)
at com.github.springtestdbunit.assertion.NonStrictDatabaseAssertion.assertEquals(NonStrictDatabaseAssertion.java:43)
at com.github.springtestdbunit.DbUnitRunner.verifyExpected(DbUnitRunner.java:130)
at com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:70)
at com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:165)
at org.springframework.test.context.TestContextManager.afterTestMethod(TestContextManager.java:416)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:91)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:233)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:176)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:77)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
我在file.xml中有多个表,其中两个是组织和触发器。 在处理组织表时,此行返回的metaData对象正好具有我期望的3列:organization_id,name和status。这些是我的组织表中的3列。
但是当处理触发器时,在同一个执行点,metaData为null,因此它继续执行到第299行并调用:
// Create metadata and cache it
metaData = new DatabaseTableMetaData(tableName, _connection, true, super.isCaseSensitiveTableNames());
返回的这个元数据有很多我在桌面上没有的列(TRIGGER_CATALOG,EVENT_MANIPULATION,ACTION_TIMING等)
预期列(我的架构中有这些列):
TRIGGER_ID
TRIGGER_EXTERNAL_ID
TRIGGER_NAME (First occurrence. See below)
STATUS
CREATED_BY
CREATED_ON
UPDATED_BY
UPDATED_ON
未知列(我的架构中没有这些):
TRIGGER_CATALOG
TRIGGER_SCHEMA
TRIGGER_NAME (Yes, This appears twice on the meta data. On the top probably from my schema, and here from reserved info/magic??)
EVENT_MANIPULATION
EVENT_OBJECT_CATALOG
EVENT_OBJECT_SCHEMA
EVENT_OBJECT_TABLE
ACTION_ORDER
ACTION_CONDITION
ACTION_STATEMENT
ACTION_ORIENTATION
ACTION_TIMING
ACTION_REFERENCE_OLD_TABLE
ACTION_REFERENCE_NEW_TABLE
ACTION_REFERENCE_OLD_ROW
ACTION_REFERENCE_NEW_ROW
CREATED
由于这个原因,它会引发异常:
org.dbunit.dataset.DataSetException: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: TRIGGER_CATALOG
at org.dbunit.database.DatabaseDataSet.getTable(DatabaseDataSet.java:323)
at com.github.springtestdbunit.assertion.NonStrictDatabaseAssertion.assertEquals(NonStrictDatabaseAssertion.java:43)
at com.github.springtestdbunit.DbUnitRunner.verifyExpected(DbUnitRunner.java:130)
at com.github.springtestdbunit.DbUnitRunner.afterTestMethod(DbUnitRunner.java:70)
at com.github.springtestdbunit.DbUnitTestExecutionListener.afterTestMethod(DbUnitTestExecutionListener.java:165)
这是否发生,因为我使用“触发器”这个词作为我的表名?在DBUnit或HSQLDB中触发它是一个保留关键字吗?如果我查看DatabaseDataSet._tableMap散列图中的其他表,那么所有其他表似乎都是健康的,并且在相应的元数据对象中具有我期望的列。 我使用的maven条目是:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>org.dbunit</groupId>
<artifactId>dbunit</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.github.springtestdbunit</groupId>
<artifactId>spring-test-dbunit</artifactId>
<version>1.1.0</version>
</dependency>
非常感谢任何帮助。
我的测试类看起来像:
public class TriggerServiceTest extends DBBaseTest {
@Autowired
private TriggerService triggerService;
@Test
@DatabaseSetup(value={"classpath:testData/trigger/Trigger_Test_2_Init.xml"}, type = DatabaseOperation.CLEAN_INSERT)
@ExpectedDatabase(value="classpath:testData/trigger/Trigger_Test_2.xml", assertionMode = DatabaseAssertionMode.NON_STRICT)
@DatabaseTearDown(value={"classpath:testData/trigger/Trigger_Test_2_Init.xml"}, type = DatabaseOperation.DELETE)
public void testTriggerWrite(){
TriggerDTO triggerDTO = new TriggerDTO();
triggerDTO.setName("Trigger_Test_1");
triggerDTO.setId("101");
String newId = triggerService.write(triggerDTO);
System.out.println("Done.");
}
}
和DBBaseTest:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:wfxSpringDatabaseTest.xml" , "classpath:wfxTriggerContextTest.xml"})
public abstract class DBBaseTest extends AbstractJUnit4SpringContextTests {
@Before
public void setApplicationContext() {
ApplicationContextHolder.setTheGlobalAppContext(applicationContext);
}
}
TESTDATA /触发/ Trigger_Test_2.xml:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<organization organization_id="1" name="MyOrg" status="Active"/>
<users user_id="1" username="admin" password="pwd123" email="abc@abc.com" full_name="The Admin" status="Active" created_on="2013-08-17 03:45:44.0" created_by="1" is_admin="1" secret_question="What is your favorite sport?" secret_answer="Quiddich" enabled="1" organization_id="1"/>
<triggers trigger_id="101" trigger_EXTERNAL_id="101" trigger_name="Trigger_101" status="Active" created_by="1" created_on="2014-08-26 11:34:25.625" updated_by="1" updated_on="2014-08-26 11:34:25.625"/>
</dataset>
TESTDATA /触发/ Trigger_Test_2_Init.xml:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<organization organization_id="1" name="MyOrg" status="Active"/>
<users user_id="1" username="admin" password="pwd123" email="abc@abc.com" full_name="The Admin" status="Active" created_on="2013-08-17 03:45:44.0" created_by="1" is_admin="1" secret_question="What is your favorite sport?" secret_answer="Quiddich" enabled="1" organization_id="1"/>
<triggers/>
</dataset>
答案 0 :(得分:0)
确认这是由于“触发器”这个词而发生的。这可能是HSQLDB中的保留关键字。将其更改为“triggers_table”后,一切正常。有关HSQLDB中所有保留关键字的信息,请参阅以下链接: