【发布时间】:2021-01-31 23:11:57
【问题描述】:
当我尝试为此 DAO 类运行单元测试时,我在 getById 返回语句处收到 NullPointerException。我知道该类没有初始化EntityManager,但我不明白为什么? - 我不知道是我的persistence.xml 配置错误还是数据库凭据不正确。
我看到了两个或更多 StackOverflow 线程,但运气不佳。我正在使用 Intellij IDE。
package com.beetlehand.model.dao;
import com.beetlehand.model.AttributeEntity;
import org.apache.commons.lang.StringUtils;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
@Stateless
public class AttributeDao extends AbstractDao<AttributeEntity> {
@PersistenceContext(unitName = "NewPersistenceUnit")
protected EntityManager entityManager;
public AttributeEntity getById(Long id) {
if(id == null) return null;
return entityManager.find(AttributeEntity.class, id);
}
/*** more code ***/
}
持久化配置文件
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="NewPersistenceUnit">
<class>com.beetlehand.model.AttributeEntity</class>
<class>com.beetlehand.model.AttributeValueEntity</class>
<class>com.beetlehand.model.AuditEntity</class>
<class>com.beetlehand.model.CategoryEntity</class>
<class>com.beetlehand.model.CustomerEntity</class>
<class>com.beetlehand.model.DepartmentEntity</class>
<class>com.beetlehand.model.OrderDetailEntity</class>
<class>com.beetlehand.model.OrdersEntity</class>
<class>com.beetlehand.model.ProductEntity</class>
<class>com.beetlehand.model.ProductAttributeEntity</class>
<class>com.beetlehand.model.ProductCategoryEntity</class>
<class>com.beetlehand.model.ReviewEntity</class>
<class>com.beetlehand.model.ShippingEntity</class>
<class>com.beetlehand.model.ShippingRegionEntity</class>
<class>com.beetlehand.model.ShoppingCartEntity</class>
<class>com.beetlehand.model.TaxEntity</class>
<properties>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/beetlehand"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/beetlehand"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="openjpa.ConnectionURL" value="jdbc:mysql://localhost:3306/beetlehand"/>
<property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/beetlehand"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="username"/>
<property name="hibernate.connection.password" value="password"/>
</properties>
</persistence-unit>
</persistence>
【问题讨论】:
-
你的测试?您是否知道您需要一些测试框架来初始化一些测试容器,然后初始化任何 EJB 等东西?简单的 - 例如 - JUnit 是不够的。
-
不,我不熟悉。我是 Java 新手。有教程可以推荐吗?
-
Java 和 JavaEE 不一样。 JavaEE 更像是对 Java 的扩展,它需要提供 CDI(注入 EntityManager)、JPA 等内容的执行环境(容器)。 SO 实际上不是任何教程参考站点,但作为开始,您可以搜索诸如 1) 单元测试和模拟 1) J2EE 集成测试之类的内容。选项 1) 意味着您可能不会实际测试与 J2EE 相关的内容,而是模拟该部分,例如 DB 查询结果。选项 2) 意味着您使用的 J2EE 测试环境与您部署应用程序的容器执行相同的操作。
-
谢谢,Pirho - 我正在阅读它
标签: java hibernate jpa intellij-idea