我正在为当地学区做一些志愿者发展。他们使用Spring和Hibernate作为基本的CRUD应用程序。我能够针对开发数据库运行一些JUnit测试,但是不想使用DbUnit对HSQLDB进行尝试。
我收到NoSuchTableException,DbUnit从XML文件加载初始数据。我已经阅读了StackOverflow上关于此的其他帖子,他们倾向于建议将Hibernate属性hibernate.hbm2ddl.auto设置为create-drop。我做到了这一点,仍然得到了例外。我想知道在设置中是否存在我缺少的基本内容。
这是我的测试用例:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:spring-test-datasource.xml",
"classpath:spring-test-dao.xml"
})
@Transactional
public class HibernateRitsAdminDaoTest {
@Autowired protected RitsAdminDao ritsAdminDao;
@Autowired protected DataSource dataSource;
protected IDatabaseTester dbTester;
@Before
public void setUp() throws Exception {
dbTester = new DataSourceDatabaseTester(dataSource);
IDataSet dataSet = new FlatXmlDataSetBuilder().build(new FileInputStream("test/RitsAdminsTest.xml"));
dbTester.setDataSet(dataSet);
dbTester.onSetup();
}
@After
public void tearDown() throws Exception {
dbTester.onTearDown();
}
@Test
public void testGetAll() throws Exception {
List<RitsAdmin> ritsAdmins = ritsAdminDao.getAll();
assertTrue(ritsAdmins.size() > 0);
}
}
这是我填充表格的DbUnit数据:
<?xml version='1.0' encoding='UTF-8'?>
<dataset>
<tbl_sn_rits_admins profile_id="1"/>
<tbl_sn_rits_admins profile_id="6"/>
<tbl_sn_rits_admins profile_id="88"/>
</dataset>
这是spring-test-datasource.xml
中的数据源定义<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:mem:schoolnet"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
这个定义了DAO和Hibernate属性的spring-test-dao.xml:
<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>us/mn/k12/district/domain/RitsAdmin.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
</props>
</property>
</bean>
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
最后,这是表的Hibernate文件:
<hibernate-mapping package="us.mn.k12.district.domain">
<class name="RitsAdmin" table="tbl_sn_rits_admins" schema="dbo" catalog="schoolnet">
<id name="profileId" type="long">
<column name="profile_id" />
</id>
</class>
</hibernate-mapping>
请注意,我将hibernate属性hbm2dll.auto设置为“create-drop”。这是其他SO帖子所暗示的。这应该在初始化期间创建表,但我得到以下堆栈跟踪:
org.dbunit.dataset.NoSuchTableException: tbl_sn_rits_admins
at org.dbunit.database.DatabaseDataSet.getTableMetaData(DatabaseDataSet.java:288)
at org.dbunit.operation.DeleteAllOperation.execute(DeleteAllOperation.java:109)
at org.dbunit.operation.CompositeOperation.execute(CompositeOperation.java:79)
at org.dbunit.AbstractDatabaseTester.executeOperation(AbstractDatabaseTester.java:190)
at org.dbunit.AbstractDatabaseTester.onSetup(AbstractDatabaseTester.java:103)
at us.mn.k12.district.dao.HibernateRitsAdminDaoTest.setUp(HibernateRitsAdminDaoTest.java:41)
... keeps going...
我缺少一个简单的配置吗? Hibernate文件中的模式和目录名称是否必须以某种方式与hsqldb URL匹配? 任何帮助表示赞赏!
答案 0 :(得分:1)
您的单元测试是否启动了hibernate框架。因为看起来你想使用Hibernate来创建表。但这只有在你的测试首先“开始”休眠时才有效。