我正在尝试测试以下类:
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();
}
现在,问题是我得到一个nullpointerexception,其堆栈跟踪看起来像:
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)
ClassA第44行是 public ClassA(){
ClassATest的第52行是 testClass = new ClassA();
我从来没有在构造函数定义中遇到过NullPointerException,所以我不知道该怎么做。任何帮助将不胜感激。
答案 0 :(得分:0)
我知道这个问题是关于NPE的,但是你应该重新考虑你的测试策略,而不是解决这个问题。为什么呢?
如果ClassA负责ClassB实例的创建和生命,并且您决定不注入ClassB的对象,为什么要尝试模拟它?您的测试不应该依赖于实现细节,因此如果您没有在任何地方注入它,就不应该模拟ClassB。
答案 1 :(得分:0)
这种做法根本不值得推荐。对象不应该负责创建自己的依赖项。它们应该在它们的构造函数中提供(或通过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;
}
}
答案 2 :(得分:-3)
我想当你宣布成员为最终时你应立即实例化它 在转到构造函数之前尝试这样做,看看会发生什么