DBUnit 错误 - 缺少以下 bean 之一:[dbUnitDatabaseConnection, dataSource]
DBUnit error - missing one the following beans: [dbUnitDatabaseConnection, dataSource]
我正在尝试将 DBUnit 与从不同配置文件(不同项目)加载的多个数据库一起使用。
在我的 Foo @Configuration 文件中,我有以下 bean:
@Bean(name="dataSourceFoo")
public DataSource dataSourceFoo() {
BasicDataSource dataSource = new BasicDataSource();
...
return dataSource;
}
在我的 Bar @Configuration 文件中,我有以下 bean:
@Bean(name="dataSourceBar")
public DataSource dataSourceBar() {
BasicDataSource dataSource = new BasicDataSource();
...
return dataSource;
}
在第三个项目中,我的测试文件是这样的:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ThirdProjectAppContextConfig.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@DatabaseSetups({
@DatabaseSetup(connection = "dataSourceFoo", value = "classpath:db/FooTest.xml"),
@DatabaseSetup(connection = "dataSourceBar", value = "classpath:db/BarTest.xml") })
@TestPropertySource("/application.properties")
@DirtiesContext
@Transactional
public class FooBarTest {
@Autowired
private Service service;
@Test
public void test(){
service.callFooBar();
...
}
}
当我 运行 测试时它抛出以下错误:
java.lang.IllegalStateException: Unable to find a DB Unit database connection, missing one the following beans: [dbUnitDatabaseConnection, dataSource]
at com.github.springtestdbunit.DbUnitTestExecutionListener.getDatabaseConnectionUsingCommonBeanNames(DbUnitTestExecutionListener.java:141)
at com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestExecutionListener.java:112)
at com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestExecutionListener.java:87)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:276)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:278)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
有人知道为什么 DBunit 找不到连接吗?
com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestContextAdapter)
正在查找数据库配置,但未找到我定义的配置,dbunit 默认使用“dataSource
”。在测试 class 上放置以下配置将解决问题:
@DbUnitConfiguration(databaseConnection={"dataSourceFoo","dataSourceBar"})
更多信息请见 Spring Test DBUnit
我正在尝试将 DBUnit 与从不同配置文件(不同项目)加载的多个数据库一起使用。
在我的 Foo @Configuration 文件中,我有以下 bean:
@Bean(name="dataSourceFoo")
public DataSource dataSourceFoo() {
BasicDataSource dataSource = new BasicDataSource();
...
return dataSource;
}
在我的 Bar @Configuration 文件中,我有以下 bean:
@Bean(name="dataSourceBar")
public DataSource dataSourceBar() {
BasicDataSource dataSource = new BasicDataSource();
...
return dataSource;
}
在第三个项目中,我的测试文件是这样的:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ThirdProjectAppContextConfig.class)
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class,
DirtiesContextTestExecutionListener.class,
TransactionalTestExecutionListener.class,
DbUnitTestExecutionListener.class })
@DatabaseSetups({
@DatabaseSetup(connection = "dataSourceFoo", value = "classpath:db/FooTest.xml"),
@DatabaseSetup(connection = "dataSourceBar", value = "classpath:db/BarTest.xml") })
@TestPropertySource("/application.properties")
@DirtiesContext
@Transactional
public class FooBarTest {
@Autowired
private Service service;
@Test
public void test(){
service.callFooBar();
...
}
}
当我 运行 测试时它抛出以下错误:
java.lang.IllegalStateException: Unable to find a DB Unit database connection, missing one the following beans: [dbUnitDatabaseConnection, dataSource]
at com.github.springtestdbunit.DbUnitTestExecutionListener.getDatabaseConnectionUsingCommonBeanNames(DbUnitTestExecutionListener.java:141)
at com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestExecutionListener.java:112)
at com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestExecutionListener.java:87)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:276)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:278)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
有人知道为什么 DBunit 找不到连接吗?
com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestContextAdapter)
正在查找数据库配置,但未找到我定义的配置,dbunit 默认使用“dataSource
”。在测试 class 上放置以下配置将解决问题:
@DbUnitConfiguration(databaseConnection={"dataSourceFoo","dataSourceBar"})
更多信息请见 Spring Test DBUnit