Mockito ExceptionInInitializerError caused by NPE



当我运行这个测试时,我得到异常初始化器错误。我认为错误在于我标有"错误"的行。也许我需要做点什么来在我的测试中处理这个陈述,但我只是想不通是什么。任何帮助将不胜感激。

我正在使用模拟来测试这一点。我没有改变DAOFactory类的自由。因此,任何更改都必须在 MyClass 中.java或在测试类中进行。

如您所见,我的测试方法是空的。

测试类:

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
    @InjectMocks MyClass myClass = new MyClass();
    @Mock DAOFactory daoFactory;
    @Mock MyDao dao;
    @Test
    public void myMethodTest(){
    }
}

要测试的类:

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
如果你想

单独测试这个类,你必须在这里删除Dao依赖项的自动实例化:

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

您可以使用构造函数重构MyClass代码,一个用于生产,一个用于测试目的:

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() { }
}

在那里,您可以避免测试中真实 DAO 的实例化问题。这就是你ExceptionInInitializerError的原因.

相关内容

  • 没有找到相关文章

最新更新