4

当我运行这个测试时,我得到了 ExceptionInitializerError。我认为错误在于我标有“错误”的行。可能是我需要做一些事情来处理我的测试中的那个陈述,但我就是不知道是什么。任何帮助都感激不尽。

我正在使用 mockito 来测试它。我无权更改 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;
    }
}

故障跟踪:

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
4

1 回答 1

1

如果你想单独测试这个类,你必须在这里删除 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.

于 2013-09-06T13:23:17.080 回答