Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBUnit error - missing one the following beans: [dbUnitDatabaseConnection, dataSource]

I'm trying to use DBUnit with multiple databases loading from different configuration files (different projects).

In my Foo @Configuration file I've the following bean:

@Bean(name="dataSourceFoo")
public DataSource dataSourceFoo() {
    BasicDataSource dataSource = new BasicDataSource();
    ...
    return dataSource;
}

In my Bar @Configuration file I've the following bean:

@Bean(name="dataSourceBar")
public DataSource dataSourceBar() {
    BasicDataSource dataSource = new BasicDataSource();
    ...
    return dataSource;
}

In a third project, my test file looks like this:

@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();
        ...
    }
}

When I run the test it throws the following error:

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$1.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$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.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)

Anyone knows why isn't DBunit finding the connections?

like image 960
bsferreira Avatar asked Sep 08 '15 19:09

bsferreira


People also ask

What is dbunitdatabaseconnection Bean?

A bean named " dbUnitDatabaseConnection " or " dataSource " is expected in the ApplicationContext associated with the test. This bean can contain either a IDatabaseConnection or a DataSource . A custom bean name can also be specified using the @DbUnitConfiguration annotation.

How to test on other databases in dbunit?

If we want to test on other databases we will need to provide our custom implementation for it. Keep in mind that, in our example, DBUnit will reinitialize the database with the given test data before each test method execution. There are multiple ways to configure this via getSetUpOperation and getTearDownOperation:

How to configure spring test dbunit to work with multiple connections?

It is possible to configure Spring Test DBUnit to work with multiple connections within the same test. First declare multiple DataSource or IDatabaseConnection beans in your application context. For example, here is XML configuration for two in-memory databases:

What is the use of @dbunitconfiguration annotation?

The @DbUnitConfiguration annotation can be used if you need to configure advanced options for DBUnit. The databaseConnection attribute allows you to specify a specific bean name from the Spring Context that contains the database connection. When not specified the names or can be used.


1 Answers

com.github.springtestdbunit.DbUnitTestExecutionListener.prepareTestInstance(DbUnitTestContextAdapter) is looking for database configurations and the ones I defined are not found and dbunit uses "dataSource" by default. Placing the following configuration on test class will solve the problem:

@DbUnitConfiguration(databaseConnection={"dataSourceFoo","dataSourceBar"})

more info at Spring Test DBUnit

like image 127
bsferreira Avatar answered Sep 29 '22 12:09

bsferreira