2015-11-26 56 views
2

我是Spring和JPA的新手。我使用Spring4,Spring Scheduler和JPA開發了一個項目,現在我想用Junit測試用例來測試我的代碼。我開始測試我的DAO方法。這裏是源代碼:實體管理器null每次運行Junit測試

此類不用於調度的觸發任務

public class TriggerTask { 


@Autowired 
private TriggerDaoBean triggerDaoBean; 

public void executeDailyTask() { 

    try{ 
    List<User> users=triggerDaoBean.getClients(); 
    if (null != users && users.size()>0){ 

     //do some task 
    } 

    }catch(Exception e){ 

    } 
}} 

的DAO bean是:

@Stateless 
public class TriggerDaoBean implements Serializable { 

    private static final long serialVersionUID = 1L; 


    @PersistenceContext 
    private EntityManager em; 

    public List<User> getUsers() {  
     List<User> userList = new ArrayList<User>();    
     Query query = em.createQuery(
       "SELECT user FROM USER user"); 
     userList = (List<User>)query.getResultList(); 
     if (null != userList && userList.size() > 0) { 
      return userList; 
     } else { 
      return null; 
     } 
    } 

    .... 
} 

我寫了下面的測試案例:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "/spring-quartz-test.xml" }) 
@TransactionConfiguration(defaultRollback=true) 
@Transactional 
public class TriggerTest { 

    @Autowired 
    TriggerDaoBean triggerDaoBean; 

    @Test 
    public void getUserListTest() {  
     triggerDaoBean.getUsers(); 
    } 
} 

spring-quartz-test.xml的內容如下:

<bean id="triggerTask" class="com.test.service.TriggerTask"> 
    <property name="triggerDaoBean" ref="triggerDaoBean"/> 
</bean> 

<bean id="triggerDaoBean" 
    class="com.test.service.TriggerDaoBean"> 
</bean> 

<bean id="runsrJobDaily" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"> 
    <property name="targetObject" ref="triggerTask" /> 
    <property name="targetMethod" value="executeDailyTask" /> 
</bean> 

而且persistence.xml中如下:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> 
    <persistence-unit name="forge-default" transaction-type="JTA" > 
    <description>Forge Persistence Unit</description> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <jta-data-source>java:/MySqlDS</jta-data-source> 


    <class>com.test.persistence.entity.User</class> 

    <properties> 
    <!-- <property name="hibernate.hbm2ddl.auto" value="create-drop"/> --> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" /> 
     <!-- 
     <property name="hibernate.ejb.cfgfile" value="modified.hibernate.cfg.xml"/> 
     --> 
     <property name="hibernate.show_sql" value="true"/> 
     <property name="hibernate.format_sql" value="true"/> 
     <property name="hibernate.transaction.flush_before_completion" value="true"/> 
    </properties> 

    </persistence-unit> 
</persistence> 

java:/MySqlDS是數據源的JNDI名稱。 每次運行我的測試時,NullPointerException都是針對EntityManager的。

我做錯了什麼?

回答

0

你必須定義在春天你的EntityManager,我沒有看到的EntityManager在您發佈的代碼,你可以做這樣

<tx:annotation-driven transaction-manager="txManager"/> 

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceUnitName" value="example" /> 
</bean> 

<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

知道,你要定義你的交易類型爲JTA在persistence.xml中,你真的需要全局事務嗎?如果不是,則刪除persistence.xml中的transaction-type="JTA"

另一件事,爲了測試目的,最好使用像H2這樣的測試數據庫。您可以在測試Spring上下文定義它裏面src/test/resources

<bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceXmlLocation" value="classpath:META-INF/persistence-test.xml"/> 
     <property name="persistenceUnitName" value="example-test" /> 
     <property name="dataSource" ref="dataSourceTest"/> 
</bean> 

<bean id="entityManager" class="org.springframework.orm.jpa.support.SharedEntityManagerBean"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
</bean> 

<bean id="dataSourceTest" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> 
     <property name="driverClass" value="org.h2.Driver"/> 
     <property name="url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE"/> 
     <property name="username" value="sa"/> 
     <property name="password" value=""/> 
</bean> 

<bean id="org.h2.tools.Server-WebServer" class="org.h2.tools.Server" 
     factory-method="createWebServer" init-method="start" lazy-init="true"> 
     <constructor-arg value="-web,-webAllowOthers,-webPort,11111" /> 
</bean> 

這種方式,您可以控制數據庫狀態,每次測試都會在內存中創建一個數據庫,你可以在src/test/resource爲了定義一個import.sql,以填補測試你的表數據。您必須在pom.xml中添加de h2依賴關係

<dependency> 
    <groupId>com.h2database</groupId> 
    <artifactId>h2</artifactId> 
    <version>1.3.174</version> 
    <scope>test</scope> 
</dependency> 
相關問題