gpt4 book ai didi

java - NPE 引起的 Mockito ExceptionInInitializerError

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:08:00 32 4
gpt4 key购买 nike

当我运行这个测试时,我得到了 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

最佳答案

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

关于java - NPE 引起的 Mockito ExceptionInInitializerError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18621763/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com