2

我正在做 DBUnit 2.5.0 和 h2 db,这是一个用于 TestNG 的内存数据库 1.4.180。我使用 DBUnit 从 db (PostgreSQL) 导出数据模式。然后我将此 xml 文件用于 H2 DB,但每当我运行 testNG 时,它都会引发异常:

org.dbunit.dataset.NoSuchTableException: result
    at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
    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 com.axonactive.TestSystemListener.TestSystemListener.beforeTestMethod(TestSystemListener.java:49)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:368)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.springTestContextBeforeTestMethod(AbstractTestNGSpringContextTests.java:159)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:653)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
    at org.testng.TestNG.run(TestNG.java:1057)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

我已经尝试了很多,但我找不到解决它的方法。请你帮助我好吗?这是我的全部课程:我创建了一个类来自定义注释:DATASET

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSet {
    String setUpDataSet() default "";
}

@DATASET 的一个侦听器类:

public class TestSystemListener implements TestExecutionListener {

    private IDatabaseTester databaseTester;

    @Override
    public void afterTestClass(TestContext testContext) throws Exception {
    }

    @Override
    public void afterTestMethod(TestContext testContext) throws Exception {
    }

    @Override
    public void beforeTestClass(TestContext testContext) throws Exception {
    }

    @Override
    public void beforeTestMethod(TestContext testContext) throws Exception {
        DataSet dataSet = testContext.getTestMethod().getAnnotation(
                DataSet.class);
        if (dataSet == null) {
            return;
        }

        String dataFile = dataSet.setUpDataSet();

        if (!dataFile.isEmpty()) {
            databaseTester = new DataSourceDatabaseTester(
                    (DataSource) testContext.getApplicationContext().getBean(
                            "dataSource"));
            databaseTester.setDataSet(getDataSet(dataFile));
            databaseTester.setSetUpOperation(getSetupOperation());
            databaseTester.onSetup();
        }
    }

    @Override
    public void prepareTestInstance(TestContext testContext) throws Exception {
    }

    protected IDataSet getDataSet(String dataFile) throws Exception {
        final FlatXmlDataSetBuilder builder = new FlatXmlDataSetBuilder();
        builder.setColumnSensing(true);
        return builder.build(new File(dataFile));
    }

    protected DatabaseOperation getSetupOperation() {
        return DatabaseOperation.CLEAN_INSERT;
    }
}

并将其应用于我的单元测试类:

@ContextConfiguration(locations = { "classpath:test-context.xml" })
@TestExecutionListeners(listeners = { TestSystemListener.class })
public class QuestionLevelTest extends
        AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private IResultService resultService;

    private final String DATASET = "./src/test/resources/database/partialdb.xml";

    @DataSet(setUpDataSet = DATASET)
    @Test
    public void addQuestionLevel() {
        Result r = new Result();
        r.setId(192019);
        Assert.assertTrue(resultService.add(r));
    }

这是我的 test-context.xml 文件:

<beans:bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <beans:property name="driverClassName" value="org.h2.Driver" />
        <beans:property name="url" value="jdbc:h2:mem:dataset;MODE=PostgreSQL" />
        <beans:property name="username" value="sa" />
        <beans:property name="password" value="" />
    </beans:bean>

    <beans:bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <beans:property name="dataSource" ref="dataSource" />
        <beans:property name="packagesToScan" value="com.axonactive.domain"></beans:property>
        <beans:property name="hibernateProperties">
            <beans:props>
                <beans:prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</beans:prop>
                <beans:prop key="hibernate.show_sql">true</beans:prop>
                <beans:prop key="hibernate.c3p0.max_statements">${hibernate.c3p0.max_statements}</beans:prop>
                <beans:prop key="hibernate.hbm2ddl.auto">create-drop</beans:prop>
            </beans:props>
        </beans:property>
    </beans:bean>

感谢您的帮助!冯阮

4

0 回答 0