Search code examples
springmockingjunit4dbunitspring-junit

How to test Dao with Dbunit for table which is not having hibernate entity


I have wrote a test class for Dao using DbUnit and dataset

Here is my class :

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:config/appContext-test.xml" })
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, DBUnitTestExecutionListener.class })
@DBUnitConfiguration(locations = { "testdata/mso_data.xml" })
public class TestMsoJobsDao{

@Resource
private MsoJobsDao msoJobsDao;

@Test
public void testSaveMsoDataIntoTempTable() throws Exception{
List<Object[]> msoHeadendList = new ArrayList<Object[]>();
Timestamp timestamp1  = Timestamp.valueOf("2015-07-01 08:49:50");
Object[] obj1 = {"TEST_MSO_SERVICE_ID_3","America/Detroit","SL","1",timestamp1,"1",timestamp1};
msoList.add(obj1);
msoJobsDao.saveMsoDataIntoTempTable(msoList);
}
}

and data set is :

<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<mso_temp id="1" mso_service_id="TEST_MSO_SERVICE_ID_3"
timezone="America/Detroit" customer_group="RC" created_by="1" 
created_date="2015-10-05 06:31:59" updated_by="1" 
updated_date="2015-10-05 06:31:59"/>
</dataset>

When i am running my test case i am getting org.dbunit.dataset.NoSuchTableException: mso_temp

My problem is i don't need any entity as i am connecting to other database and saving the data from there to the temp table in our application database using PreparedStatement.If i create entity class the test case runs fine.

Is there any way through which DBUnit will consider the table for which entity class is not there.


Solution

  • dbUnit does not use entity classes, it directly uses JDBC. The dbUnit error is table does not exist, and dbUnit does not create tables. Your application / test setup creates the tables from the entity classes, so without the entity, the table does not exist.

    For needed tables without entities, test setup must create these tables. For ease, you may want to just put the entity in the test classes folder. Another option is to run DDL that creates the table(s) before running all tests.