我正在模拟类模拟阶级的对象,并在称为其中一种方法时使用何时返回SomeClassObject(这是MethodTobeTested的内部)。现在,在下面的代码的最后一行中,当我打电话给MethodTobeTested时,我希望对SomeMethod的内部呼叫能够成功运行。但是,有一个无效的interexception发生在对召唤的呼叫线中。为什么我的无类型仍然为null?我正在使用@runwith(MockitoJunitrunner.class),如许多线程中所示。
编辑:根据@dawood,我用@InjectMocks oighclass oighobject替换了@mock oighclass obleobject。我下面的输出是在此更换之后。我的想法是,在以下行中未执行somemethod。当(MockObject.SomeMeThod(arg1,arg2)但是它确实被执行,我在其中有一个例外。我在这里做什么,因为我只想模拟该方法并定义其给出的输出而不是调用它
@RunWith(MockitoJUnitRunner.class)
public testClassToBeTested{
@Mock
MockClass mockObject;
@Before
public void setUp() throws Exception {
Whitebox.setInternalState(ClassToBeTested, "argumentName",
arg2);
}
@Test
public void methodToBeTested{
Class1 arg1 = new Class1();
Class1.setUserId("test");
//someMethod which is called within methodToBeTested
when(mockObject.someMethod(arg1,arg2).thenReturn(someClassObject));
ClassAnotherClass anotherClassObject = ClassToBeTested.methodToBeTested(arg1);
}
}
控制台输出:
java.lang.NullPointerException
at com.mycompany.utils.MockClass.someMethod(MockClass.java:36)
at com.mycompany.impl.ClassToBeTested.methodToBeTested(ClassToBeTested.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.rules.ExpectedException$ExpectedExceptionStatement.evaluate(ExpectedException.java:239)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
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:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
很难说出您的代码示例未完成,而不是直接复制,并且您也没有粘贴NPE或发生的线路。机会是:
模拟未注入
在这种情况下,您可以将@inject放在类的构造函数上,并在测试中手动注入模拟,以确保。
模拟返回null
如果模拟参数不匹配,则可能发生这种情况,或模拟代码中的其他错误。
在示例中,您无法正确模拟:
when(mockObject.someMethod(arg1,arg2).thenReturn(someClassObject));
应该是
when(mockObject.someMethod(arg1,arg2)).thenReturn(someClassObject);
注意括号的更改