Susie
Susie

Reputation: 5148

Mockito ExceptionInInitializerError caused by NPE

When I run this test I get ExceptionInitializerError. I think the error lies at line I have with marked with "Error". May be I need to do something to take care of that statement in my test but I just can't figure out what. Any help will be much appreciated.

I am using mockito to test this. I don't have the liberty to change the DAOFactory class. So any changes have to be in MyClass.java or in the test class.

As you can see my test method is empty.

Test class:

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @InjectMocks MyClass myClass = new MyClass();

    @Mock DAOFactory daoFactory;
    @Mock MyDao dao;

    @Test
    public void myMethodTest(){

    }
}

Class to be tested:

public class MyClass{   
    private MyDao dao = DAOFactory.getInstance().getMyDao();    
    public void myMethod(){
    }
}

public class DAOFactory {
    private static DAOFactory instance = new DAOFactory();
    private DAOFactory() {}

    public static DAOFactory getInstance() {
        return instance;
    }

    final private CustDao custDao= new CustDao();//-------Error-----------
    final private MyDao dao = new MyDao();//-------Error-----------

    public MyDao getMyDao(){
        return dao;
    }
}

Failute trace:

java.lang.ExceptionInInitializerError
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:222)
    at com.mycomp.nom.service.impl.MyClass.<init>(MyClass.java:15)
    at com.mycomp.nom.service.impl.IHardCloseoutTimeServiceImplTest.<init>(IHardCloseoutTimeServiceImplTest.java:15)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:44)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:516)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:195)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:244)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:241)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
    at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.NullPointerException
    at com.mycomp.core.persistence.hibernate.AbstractDAO.<init>(AbstractDAO.java:71)
    at com.mycomp.bbm.core.persistence.hibernate.AbstractDAO.<init>(AbstractDAO.java:58)
    at com.mycomp.nom.dao.hibernate.AbstractTDAO.<init>(AbstractTDAO.java:52)
    at com.mycomp.nom.dao.hibernate.HibernateAggregateMailClassDAO.<init>(HibernateAggregateMailClassDAO.java:38)
    at com.mycomp.nom.dao.DAOFactory.<init>(DAOFactory.java:51)
    at com.mycomp.nom.dao.DAOFactory.<clinit>(DAOFactory.java:42)
    at java.lang.J9VMInternals.initializeImpl(Native Method)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:200)
    ... 26 more

Upvotes: 4

Views: 9578

Answers (1)

bric3
bric3

Reputation: 42273

If you want to test this class in isolation you have to remove the automatic instantiation of the Dao dependency here :

public class MyClass{   
    private MyDao dao = DAOFactory.getInstance().getMyDao();    
    public void myMethod(){
    }
}

You could refactor MyClass code with constructors, one for production one for test purpose :

public class MyClass{   
   private MyDao dao;

   public MyClass() {
      dao = DAOFactory.getInstance().getMyDao();
   }

   @VisibleForTesting
   MyClass(Dao dao) {   // package visible for the test only
      this.dao = dao;
   }

   public void myMethod() { }
}

There you can avoid the problematic instantiation of the real DAO in your test. Which was the reason why you had a ExceptionInInitializerError.

Upvotes: 1

Related Questions