Search code examples
javamavenjdbcclasspathdbunit

NoSuchTableException using dbunit to test dao class


I'm using dbunit with an h2 in-memory database to test the methods in a DAO class that I wrote. All tests in the suite used to pass successfully, but then I reorganized the directory structure of the project and now I'm getting a NoSuchTableException when I run the test suite. I feel that it's got to be some kind of build path error, but I've been banging my head over it for two days now and can't fix it.

Here's an excerpt from the test class containing some of the set-up methods:

@RunWith(PowerMockRunner.class)
@PrepareForTest(DAOUtilities.class)
public class FeedingScheduleDaoImplDBUnitTest extends DataSourceBasedDBTestCase {

    private Connection connection;

    private FeedingScheduleDaoImpl fsdi = new FeedingScheduleDaoImpl();

    @Override
    protected DataSource getDataSource() {
        JdbcDataSource dataSource = new JdbcDataSource();
        dataSource.setUrl("jdbc:h2:mem:default;DB_CLOSE_DELAY=-1;init=runscript from 'classpath:schema.sql'");
        dataSource.setUser("sa");
        dataSource.setPassword("sa");
        return dataSource;
    }

    @Override
    protected IDataSet getDataSet() throws Exception {
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream("data.xml");
        return new FlatXmlDataSetBuilder().build(inputStream);
    }

    @Override
    protected DatabaseOperation getSetUpOperation() {
        return DatabaseOperation.REFRESH;
    }

    @Override
    protected DatabaseOperation getTearDownOperation() {
        return DatabaseOperation.DELETE_ALL;
    }
    

    @Before
    public void setUp() throws Exception {
        super.setUp();
        connection = getConnection().getConnection();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void givenDataSetEmptySchema_whenDataSetCreated_thenTablesAreEqual() throws Exception {
        IDataSet expectedDataSet = getDataSet();
        ITable expectedTable = expectedDataSet.getTable("animals");
        IDataSet databaseDataSet = getConnection().createDataSet();
        ITable actualTable = databaseDataSet.getTable("animals");
        Assertion.assertEquals(expectedTable, actualTable);
    }
}

It's the return new FlatXmlDataSetBuilder().build(inputStream); that is actually generating the exception, but I suspect that's because the schema isn't being properly loaded in getDataSource() method.

Below is my new directory structure after the reorganization. The sql schema file and the xml data files needed for the tests are all in the src/test/resources directory.

eZoo directory structure

And here's the build section of my pom.xml. I haven't specified any source or resource directories for the build, and so if I'm understanding things correctly, that should land all of the resource files in the default target/test-classes directory. I've checked and they are in fact there.

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
            </plugin>
        </plugins>
    </build>

Any tips or help would be much appreciated. I'm still learning how to use Maven, and I did this reorganization in an effort to better understand the standard Maven project structure, so I'm hoping to both fix what I broke and learn how to use Maven better.


Solution

  • After tinkering, I found a solution to my problem. I removed the following line from my xml files:

    <!DOCTYPE xml>
    

    Here's a link to another question and answer where I found the solution: https://stackoverflow.com/a/5915063.