2014-09-05 84 views
0

是否可以在JUNIT的插入/更新中测试插入?spring + hibernate测试服务/ dao插入

我为spring-servlet.xml

<context:component-scan base-package="com.isad" /> 
<mvc:annotation-driven /> 

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
<property name="prefix"> 
    <value>/WEB-INF/jsp/</value> 
</property> 
<property name="suffix"> 
    <value>.jsp</value> 
</property> 
</bean> 

<bean id="sessionFactory" scope="singleton" 
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager"/> 
<bean id ="transactionManager" class = "org.springframework.orm.hibernate4.HibernateTransactionManager"> 
<property name = "sessionFactory" ref = "sessionFactory"/> 
</bean> 

我的测试配置:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"classpath:spring-servlet.xml"}) 
@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true) 
@Transactional 
public class TestItem { 

@Autowired CategoryService catManager; 

@Test 
public void testItem() { 
    //System.out.println("Begin Test"); 
    Category cat = new Category(); 
    cat.setType(CategoryType.BOOK); 
    cat.setSubCategory("Business Book"); 
    cat.setDescription("One Thing"); 
    catManager.addCategory(cat); 
} 
} 

这是我得到的运行上面的代码stackstrace:

Hibernate: insert into CATEGORY (DESCRIPTION, SUB_CAT, CAT_TYPE) values (?, ?, ?) 
INFO | 2014-09-04 18:46:50,844 | TransactionalTestExecutionListener.java | 298 | Rolled back transaction after test execution for test context [[email protected] testClass = TestItem, testInstance = [email protected], testMethod = [email protected], testException = [null], mergedContextConfiguration = [[email protected] testClass = TestItem, locations = '{classpath:spring-servlet.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]] 
INFO | 2014-09-04 18:46:50,847 | AbstractApplicationContext.java | 873 | Closing [email protected]372afb: startup date [Thu Sep 04 18:46:48 PDT 2014]; root of context hierarchy 

问题我有插入和更新旁边,我的其他方法,如删除工作得很好。

+1

发布完整堆栈跟踪 – 2014-09-05 03:30:33

回答

1

您的测试成功了,但由于这是一项测试,并且您指定了defaultRollback=true,所以当测试方法结束时,对数据库所做的任何更改都会回滚。

我会假设你的服务,你有一个运行COUNT(*)声明此Category对象 等效的方法。如果你真的想测试您插入成功,请尝试以下代码:

@Test 
public void testInsertItem(){ 
    int countPreInsert = catManager.count(); 

    Category cat = new Category(); 
    cat.setType(CategoryType.BOOK); 
    cat.setSubCategory("Business Book"); 
    cat.setDescription("One Thing"); 
    catManager.addCategory(cat); 

    int countPostInsert = catManager.count(); 

    assertEqual("Exactly one record was not inserted in the database", 
     countPreInsert + 1, countPostInsert) 
} 

您不必进行任何清理,因为TransactionalTestExecutionListener将在测试结束时自动回滚插入语句。

如果要测试更新,请在更新前后进行计数,并断言它们都相同,以确保Hibernate不会插入或删除任何记录。

快乐测试!

+0

对于更新,您还可以使用嵌入式'JdbcTemplate'并执行'queryforMap',它可以为您提供一个键/值对,然后您可以使用该断言值进行检查。我总是建议使用另一种方法(JDBC)来测试你的假设,然后你正在使用的假设(JPA/Hibernate),它可能会返回一个正确的结果(由于缓存),但它尚未同步到期缺乏tx设置。 – 2014-09-05 07:04:43

+0

啊,但Hibernate足够聪明,可以想到这一点。无论何时调用count(*),Hibernate都会将底层会话刷新到数据库,这样就不会出现陈旧的结果 – JamesENL 2014-09-05 07:18:37

+0

理论上说,如果开始搞乱flushmodes,hibernate就不再那么聪明了。 – 2014-09-05 07:20:11