springtestdbunit / spring-test-dbunit

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

Using dbunit-datasetbuilder #69

Closed manikantag closed 8 years ago

manikantag commented 9 years ago

HI, I m trying to create IDataSet programatically instead of xml datasets. For that I m using https://github.com/opensource21/dbunit-datasetbuilder (or https://github.com/marcphilipp/dbunit-datasetbuilder). How ever, the assertion fails.

Below is the code:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/services-test-context.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, DirtiesContextTestExecutionListener.class, TransactionalTestExecutionListener.class, TransactionDbUnitTestExecutionListener.class })
@DatabaseSetup("/dummydata.xml") // without some valid file path, test is failing!
@DbUnitConfiguration(dataSetLoader = DBUnitTest.LaunguageDataSetLoader.class)
public class DBUnitTest {

    @Test
    @ExpectedDatabase(value = "/datasets/testdata.xml", table = "language")
    public void test() {
        System.out.println("Success");
    }

    public static class LaunguageDataSetLoader extends AbstractDataSetLoader {

        @Override
        protected IDataSet createDataSet(Resource resource) throws Exception {
            DataSetBuilder builder = new DataSetBuilder();

            builder.newRow("language").with("id", 12).with("version", 0).with("name", "English").add();

            return builder.build();
        }

    }

}

As my dataset comes from the LaunguageDataSetLoader class, I wanted to NOT to give any dataset.xml path to @DatabaseSetup. But this annotation mandates something for value. When I've given empty string, like @DatabaseSetup(""), it is giving me other exceptions.

To work around this, I m giving dummydata.xml like below:

<?xml version="1.0" encoding="UTF-8"?>
<dataset>
</dataset>

If I leave the @DatabaseSetup annotation, I m getting junit.framework.ComparisonFailure: row count (table=language) expected:<[1]> but was:<[0]> failure. I think this is expected only.

Basically, my objective is to provide dataset from with in the class (this will avoid the need for ReplacementDataSet for currenttime or null, ...).

My question is, can we not make the value of the annotation optional, and also why is the annotation is supported at class level. Even better, having dataSetLoader support at @DatabaseSetup level itself instead of @DbUnitConfiguration.

philwebb commented 9 years ago

The design of DataSetLoader originally assumed that it would only be used to support a different dataset format. I've not seen dbunit-datasetbuilder before and it never occurred to me that someone might want to work this way.

It should be pretty easy to make the @DatabaseSetup.value() optional and add a new loader() attribute.

If you have time to put together a pull-request that would be very helpful.

vanackej commented 9 years ago

@manikantag I made a PR a few month ago, still waiting for feedback on it : https://github.com/springtestdbunit/spring-test-dbunit/pull/72

philwebb commented 8 years ago

PR #72 has now been merged to master