Skip to content

How to add DBUnit data through before running all tests? #163

@Freeongoo

Description

@Freeongoo

I have integration tests. But before running all the tests, I have to insert general data. They are needed only for tests, for example, add all rights for a particular user. Of course, I cannot set up a production database with these rights.

As a solution, I created a separate dbunit file with common data that should be inserted before running all the tests: "common_inserts.xml" But the problem is that I don’t want to duplicate the code and add this file to each test class, too much duplication.

I also have an abstract base test from which absolutely all tests are inherited. And it would be right to add a common data insert in this class:

@Transactional
@SpringBootTest
@RunWith(SpringRunner.class)
@TestExecutionListeners(value = {
        TransactionalTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class,
        DbUnitTestExecutionListener.class
}, mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS)
@DatabaseSetup({"/dbunit/common_inserts.xml"})
public abstract class AbstractCommonIntegrationTest {
    // ...
}

@DatabaseSetup({"/dbunit/my_test.xml"})
public class MyTest extends AbstractCommonIntegrationTest {
    // not inserted data from "/dbunit/common_inserts.xml"
}

But the problem is it doesn't work! No data is inserted that was specified in the abstract base class. Is it possible to solve this problem?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions