我正在使用JUnit
编写Mockito
个测试用例。我使用带有IIOImage类java的模拟对象的反射填充了一个列表。虽然我的列表有对象,但我的测试用例失败了,我得到一个空指针异常。有人可以帮忙吗?
_sourceList=(List) Whitebox.getInternalState(OneClass, "_sourceListContent");
_sourceList.add(Mockito.mock(IIOImage.class));
_sourceList.add(Mockito.mock(IIOImage.class));
Field listfield=OneClass.class.getDeclaredField("_sourceListContent");
listfield.setAccessible(true);
listfield.set(oneclass, _sourceImages);
System.out.println(_sourceList.size()+"is size");
for(int i=0;i<=_sourceImages.size(); i++) //this loop is giving null
{
IIOImage img = (IIOImage)_sourceImages.get(i);
SomeClass data=Mockito.mock(SomeClass.class);
data=(SomeClass)img.getMetadata();
data.dumpData();
}
它返回此处的大小,但是当我运行测试用例说空指针时会导致问题。
这里是堆栈跟踪
java.lang.NullPointerException
at SomeClass.SomeClassTest.dumpRequestTest(IPRequestTest.java:285)
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:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
答案 0 :(得分:3)
您正在创建实例作为模拟:
_sourceList.add(Mockito.mock(IIOImage.class));
稍后您逐个退出,并在每个方法上调用此方法:
IIOImage img = (IIOImage)_sourceImages.get(i);
data=(SomeClass)img.getMetadata();
调用此方法时,你没有声明Mockito应该返回什么(使用when(..)。thenReturn(..)语法),因此Mockito返回一个默认的空值。
然后你打电话给:
data.dumpData();
NullPointerException
data
为null
。
<强>修正强>
when(img.getMetadata()).thenReturn(..)
声明应返回的内容。