2014-09-04 149 views
1

对于我来说,配置服务和dao层的单元测试是一个很大的挑战。我试图遵循一些教程,但我仍然无法使测试工作。测试春天hibernate dao和服务层

我UserAccountService:

@Transactional(readOnly=true, rollbackFor={UserAccountNotFoundException.class})  
public UserAccount findById(int id) throws UserAccountNotFoundException { 
    LOGGER.debug("Find an UserAccount entry with id: {}" + id); 
    return userDao.findById(id); 
} 

我的UserDAO

public UserAccount findById(int id) { 
    return (UserAccount) session.get(UserAccount.class, id); 
} 

我的弹簧servlet.xml中:

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

<mvc:resources mapping="/resources/**" location="/WEB-INF/" cache-period="31556926"/> 

<!-- 
    Initialize base viewers 
--> 
<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> 

<!-- 
    Error Messages Handling 
--> 
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages" /> 
    <property name="defaultEncoding" value="UTF-8" /> 
</bean> 


<!-- 
    Enable Data Transaction to Database. 
--> 
<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 TestUserAccountDao { 
@Autowired 
UserAccountService userManager; 

@Test 
@Transactional 
public void testFindUser() throws UserAccountNotFoundException {  
    UserAccount other = userManager.findById(1); 
    System.out.println("Hello User: " + other.getUsername()); 
} 

我现在有当我运行上述测试问题:

Testing begin 
Hibernate: select this_.USER_ID as y0_, this_.USERNAME as y1_, this_.EMAIL as y2_, this_.DISPLAYNAME as y3_ from USER_ACCOUNT this_ where this_.USER_ID=? and this_.ENABLED=? and this_.role=? 
UserAccount [id=0, username=null, email=null, password=null, firstname=null, lastname=null, displayname=null, fullName=null, phone=null, fax=null, role=CUSTOMER, enabled=true, createOn=Thu Sep 04 13:45:31 PDT 2014] 
INFO | 2014-09-04 13:45:31,084 | TransactionalTestExecutionListener.java | 298 | Rolled back transaction after test execution for test context [[email protected] testClass = TestUserAccountDao, testInstance = [email protected], testMethod = [email protected], testException = java.lang.NullPointerException, mergedContextConfiguration = [[email protected] testClass = TestUserAccountDao, locations = '{classpath:spring-servlet.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]] 
INFO | 2014-09-04 13:45:31,088 | AbstractApplicationContext.java | 873 | Closing [email protected]5a80df: startup date [Thu Sep 04 13:45:28 PDT 2014]; root of context hierarchy 

我的hibernate.cfg.xml和弹簧servlet.xml中位于在src /主/资源。

回答

1

你在你的测试中得到了一个N​​PE,并且是一个RuntimeException,TransactionalTestExecutionListener已经捕获它并且回滚你当前的执行事务。

因为我没有看到任何DBUnit的或任何其他代码中插入与ID = 1的用户,我只能假设你没有在你的数据库的用户,这就是为什么:

session.get(UserAccount.class, id); 

返回null。

尝试修改此:

System.out.println("Hello User: " + other.getUsername()); 

if(other != null) { 
    System.out.println("Hello User: " + other.getUsername()); 
} else { 
    System.out.println("No User found for id : 1 "); 
} 
+0

谢谢你的建议。你是完全正确的。该ID从不同的号码开始 – 2014-09-04 22:04:35