gpt4 book ai didi

java - 每次运行 Junit 测试时实体管理器都为空

转载 作者:行者123 更新时间:2023-11-30 10:53:35 25 4
gpt4 key购买 nike

我是 Spring 和 JPA 的新手。我使用 Spring4 、 spring scheduler 和 JPA 开发了一个项目,现在我想使用 Junit 测试用例来测试我的代码。我开始测试我的 DAO 方法。这是源代码:

这个类为调度器做触发任务

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 名称。每次我运行测试时,EntityManager 都会出现 NullPointerException。

我做错了什么吗?

最佳答案

我是我的情况,你需要在你的测试类上附加两个注释。

import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootApplicationClass.class)

关于java - 每次运行 Junit 测试时实体管理器都为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33935146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com