dbunit如何设置属性来测试在docker容器中运行的数据库

q3qa4bjr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(276)

我正在构建一个使用mysql数据库的spring启动应用程序。我可以使用dbunit运行集成测试来测试数据库,但是如果在docker容器中运行同一个数据库,dbunit将失败并出现错误:

org.dbunit.dataset.NoSuchTableException: Did not find table 'spring_session_attributes' in schema 'null'

    at org.dbunit.database.DatabaseTableMetaData.<init>(DatabaseTableMetaData.java:146)
    at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:316)
    at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
    at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
    at com.github.springtestdbunit.DbUnitRunner.setupOrTeardown(DbUnitRunner.java:183)
    at com.github.springtestdbunit.DbUnitRunner.beforeTestMethod(DbUnitRunner.java:75)
    at com.github.springtestdbunit.DbUnitTestExecutionListener.beforeTestMethod(DbUnitTestExecutionListener.java:185)
    at org.springframework.test.context.TestContextManager.beforeTestMethod(TestContextManager.java:269)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.RunPrepareTestInstanceCallbacks.evaluate(RunPrepareTestInstanceCallbacks.java:64)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.springframework.test.context.junit4.statements.SpringFailOnTimeout.evaluate(SpringFailOnTimeout.java:87)
    at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:101)
    at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:449)
    at junitparams.JUnitParamsRunner.runChild(JUnitParamsRunner.java:393)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:101)
    at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:242)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

我的模式中的每个表都会引发此错误。请注意,在没有docker的情况下运行数据库时,不会出现此问题。
同时我可以通过mysql workbench访问docker容器中的数据库,看到数据库存在,所有表都存在。我在测试中的设置是:

@Before
    public void setup() {
       System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
                "com.mysql.jdbc.Driver");
     System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
                "jdbc:mysql://localhost:3306/portal");
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
                "root");
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
                "pass");
        System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA,
                "portal");

        ...
    }

看来问题应该出在连接的某个地方 jdbc:mysql://localhost:3306/portal ,但我不确定,因为似乎连接已建立,但“对话”已中断。我必须提到的是,项目本身可以很好地处理docker容器中的数据库。如果有人能帮助我或指导我一点,我将不胜感激。

3duebb1j

3duebb1j1#

dbunit不支持mysql connector java(版本高于5.1),请点击此处!
我们使用自定义dbunit的org.dbunit.ext.mysql.imetadatahandler(介于defaultmetadatahandler和mysqlmetadatahandler之间,混合方法)运行spring boot 2.1.3、mysql connector java 8.0.15、database rider 1.5.2。docker中的mysql数据库。很好用。

相关问题