1

我使用 DbUnit 和数据集为 Dao 编写了一个测试类

这是我的课:

@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);
}
}

数据集是:

<?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>

当我运行我的测试用例时,我得到 org.dbunit.dataset.NoSuchTableException: mso_temp

我的问题是我不需要任何实体,因为我正在连接到其他数据库并使用 PreparedStatement 将数据从那里保存到我们的应用程序数据库中的临时表中。如果我创建实体类,则测试用例运行良好。

有什么方法可以让 DBUnit 考虑不存在实体类的表。

4

1 回答 1

2

dbUnit 不使用实体类,它直接使用 JDBC。dbUnit 错误是表不存在,dbUnit 不创建表。您的应用程序/测试设置从实体类创建表,因此没有实体,表不存在。

对于没有实体的所需表,测试设置必须创建这些表。为方便起见,您可能只想将实体放在测试类文件夹中。另一种选择是在运行所有测试之前运行创建表的 DDL。

于 2016-06-09T12:44:43.453 回答