【问题标题】:How to test Dao with Dbunit for table which is not having hibernate entity如何使用 Dbunit 测试没有休眠实体的表的 Dao
【发布时间】:2016-06-08 07:14:01
【问题描述】:

我已经使用 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 有什么方法可以考虑不存在实体类的表。

【问题讨论】:

    标签: spring mocking junit4 dbunit spring-junit


    【解决方案1】:

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

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

    【讨论】:

    • 杰夫感谢您的回复,这意味着如果我将在测试文件夹中创建实体类,它不会造成任何问题。
    猜你喜欢
    • 1970-01-01
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多