जावा EntityManager NullPointerException को फेंक रहा है
Oct 17 2020
जब मैं इस DAO वर्ग के लिए यूनिट परीक्षण चलाने की कोशिश करता हूं, तो मुझे NBPointerException getById रिटर्न स्टेटमेंट में मिल रहा है। मुझे पता है कि वर्ग EntityManager को आरंभीकृत नहीं करता है, लेकिन मुझे समझ नहीं आता कि क्यों? - मैं नहीं बता सकता कि क्या मेरी दृढ़ता। xml कॉन्फ़िगरेशन गलत है या DB क्रेडेंशियल गलत हैं।
मैंने दो या अधिक स्टैकऑवरफ्लो थ्रेड्स देखे, लेकिन बहुत कम किस्मत थी। मैं 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>
जवाब
SSK Oct 18 2020 at 14:25
आपको नीचे दिए गए entityManagerशब्दों के entityManager.find()अनुसार स्टब करने की आवश्यकता है ।
@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);