我是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的。
我做錯了什麼?