springtestdbunit / spring-test-dbunit

Integration between the Spring testing framework and DBUnit
http://springtestdbunit.github.com/spring-test-dbunit/
Apache License 2.0
476 stars 238 forks source link

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

Open Freeongoo opened 5 years ago

Freeongoo commented 5 years ago

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?

ppodgorsek commented 5 years ago

This issue relates to another one: https://github.com/springtestdbunit/spring-test-dbunit/issues/113 It was fixed in a maintained repository: https://github.com/ppodgorsek/spring-test-dbunit