0

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.

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 ***/ 
}

Persistence configuration file

<?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>
dobby
  • 23
  • 6
  • 1
    Your test? Are you aware that you need some test framework that initializes some test container that then initializes any EJB etc. stuff? Plain -for example- JUnit is not enough. – pirho Oct 17 '20 at 19:19
  • No, I am not familiar. I am new to Java. Is there a tutorial that you can recommend? – dobby Oct 17 '20 at 19:26
  • Java & JavaEE are not the same. JavaEE is more like an extension to Java that requires execution environment (container) that provides stuff like CDI (inject EntityManager), JPA and so on. SO is not actually any tutorial reference site but as a start you could search stuff like 1) unit testing and mocking 1) J2EE integration testing. Option 1) means that you might not actually test things related to J2EE but mock that part, like DB query results. Option 2) means that you use J2EE test environment that does the same stuff that the container you deploy your app. – pirho Oct 17 '20 at 19:43
  • Thanks, Pirho - I am reading into it – dobby Oct 17 '20 at 19:53

1 Answers1

0

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);

SSK
  • 3,444
  • 6
  • 32
  • 59