DbUnit-常见错误解决方法

作者:聂勇 欢迎转载,请保留作者信息并说明文章来源!

环境

  • dbunit-2.4.5

一、AmbiguousTableNameException

详细的错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
org.dbunit.database.AmbiguousTableNameException: AREA_GROUP
at org.dbunit.dataset.OrderedTableNameMap.add(OrderedTableNameMap.java:198)
at org.dbunit.database.DatabaseDataSet.initialize(DatabaseDataSet.java:227)
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:275)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:156)
at cn.aofeng.dbunit.PersonalTester.setUp(PersonalTester.java:40)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

解决方法:指定schema。
例如:System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA, “aidmcu”);

二、NoPrimaryKeyException

详细的错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
org.dbunit.dataset.NoPrimaryKeyException: PERSONAL
at org.dbunit.operation.UpdateOperation.getOperationData(UpdateOperation.java:72)
at org.dbunit.operation.RefreshOperation$UpdateRowOperation.<init>(RefreshOperation.java:266)
at org.dbunit.operation.RefreshOperation.createUpdateOperation(RefreshOperation.java:142)
at org.dbunit.operation.RefreshOperation.execute(RefreshOperation.java:100)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:156)
at cn.aofeng.dbunit.PersonalTester.setUp(PersonalTester.java:42)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

解决方法:给表 PERSONAL 增加主键。

三、UnsupportedOperationException: Only one iterator allowed!

详细的错误信息如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
java.lang.UnsupportedOperationException: Only one iterator allowed!
at org.dbunit.dataset.stream.StreamingDataSet.createIterator(StreamingDataSet.java:71)
at org.dbunit.dataset.AbstractDataSet.iterator(AbstractDataSet.java:187)
at org.dbunit.operation.AbstractBatchOperation.iterator(AbstractBatchOperation.java:93)
at org.dbunit.operation.AbstractBatchOperation.execute(AbstractBatchOperation.java:129)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at org.dbunit.DatabaseTestCase.setUp(DatabaseTestCase.java:156)
at cn.aofeng.dbunit.PersonalTester.setUp(PersonalTester.java:41)
at junit.framework.TestCase.runBare(TestCase.java:125)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

原因:XML内容格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<table name="SRC_FW_PKG">
<column>PKG_ID</column>
<column>PKG_TYPE</column>
<column>MODEL_ID</column>
<column>MANU_ID</column>
<column>SOURCE_VER</column>
<column>TARGET_VER</column>
<column>PKG_SIZE</column>
<row>
<value>1</value>
<value>1</value>
<value>6111</value>
<value>396</value>
<value>VER1.1</value>
<value>VER1</value>
<value>396</value>
</row>
</table>
</dataset>

却用 FlatXmlDataSet 来解析。

解决方法:用 XmlDataSet 解析XML文件。
产生 UnsupportedOperationException 的有多种原因,这里的解决方法仅供参考。