Java EntityManager抛出NullPointerException [英] Java EntityManager throwing NullPointerException

查看:96
本文介绍了Java EntityManager抛出NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试为此DAO类运行单元测试时,在getById返回语句中得到NullPointerException.我知道该类不会初始化EntityManager,但是我不明白为什么? -我无法确定我的persistence.xml配置是否错误或数据库凭据是否正确.

When I try to run the Unit test for this DAO class, I am getting NullPointerException at getById return statement. I know that the class does not initialize the EntityManager, but I don’t understand why? - I can’t tell whether my persistence.xml configuration is wrong or the DB credentials are incorrect.

我看到了两个或多个StackOverflow线程,但是运气不佳.我正在使用Intellij IDE.

I saw two or more StackOverflow threads but had had little luck. I am using 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>

推荐答案

您需要模拟entityManager,然后需要对entityManager.find()进行存根,如下所示.

You need to mock the entityManager then you need to do stub for entityManager.find() as shown below.

@Mock // Mocking enitt
private EntityManager entityManager;

public AttributeEntity entity = new AttributeEntity();
// Stubbing for entityManager.find()
Mockito.when(entityManager.find(Mockito.any(AttributeEntity.class), Mockito.any())).thenReturn(entity);

这篇关于Java EntityManager抛出NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆