我正在尝试测试以下类:
public final class ClassA {
private final ClassB member;
public ClassA() {
this.member = new ClassB();
}
}
以下是测试类:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ClassA.class, ClassB.class})
public final class ClassATest {
@Mock
private ClassB mocked;
private ClassA testClass;
@Before
public void initTest() {
PowerMockito.whenNew(ClassB.class).withNoArguments().thenReturn(mocked);
testClass = new ClassA();
}
现在,问题是我得到一个空指针异常,其堆栈跟踪如下所示:
java.lang.NullPointerException
[junit] at a.a.i.ClassA.<init>(ClassA.java:44)
[junit] at a.a.i.ClassATest.init(ClassATest.java:53)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[junit] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
[junit] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[junit] at java.lang.reflect.Method.invoke(Method.java:606)
[junit] at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132)
[junit] at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
[junit] at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
[junit] at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
[junit] at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
[junit] at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
[junit] at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
[junit] at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
[junit] at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
[junit] at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
[junit] at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:38)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:532)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1165)
[junit] at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:988)
A类的44号线是 public ClassA() {
而 ClassATest 的第 52 行是 测试类 = 新的类 A();
我以前从未在构造函数定义中遇到过NullPointerException,所以我不知道该怎么做。任何帮助将不胜感激。
这个问题是关于NPE的,但与其解决这个问题,不如重新考虑你的测试策略。为什么?
如果 ClassA 负责 ClassB 实例的创建和生存,而你决定不注入 ClassB 的对象,为什么你试图嘲笑它?您的测试不应依赖于实现细节,因此如果您没有在任何地方注入 ClassB,则不应模拟它。
这种方法根本不值得推荐。对象不应负责创建自己的依赖项。它们应该在其构造函数中提供(或通过 setter 方法提供)。
我建议您像这样更改构造函数
public final class ClassA {
private final ClassB member;
public ClassA(ClassB member) {
this.member = member;
}
}
如果你想保留你的代码,你可以添加一个在测试中使用的构造函数(尽管也不建议只为测试目的添加代码)
public final class ClassA {
private final ClassB member;
public ClassA() {
this(new ClassB());
}
// Default visibility so it's only visible in its package
// Added for testing purposes
ClassA(ClassB member) {
this.member = member;
}
}
我想当您将成员声明为最终时,您应该立即实例化它尝试在转到构造函数之前执行此操作,看看会发生什么