我正在使用Mockito
编写JUnit
测试用例。我已经用java的IIOImage类的mock对象使用反射填充了一个列表。尽管我的列表中有对象,但我的测试用例失败了,我得到了一个空指针异常。有人能帮忙吗?
_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();
}
它在这里返回大小,但当我运行测试用例时说null指针会导致问题。
这是堆栈跟踪
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)
您正在创建模拟实例:
_sourceList.add(Mockito.mock(IIOImage.class));
稍后,您将一个接一个地退休,并在每个上调用此方法:
IIOImage img = (IIOImage)_sourceImages.get(i);
data=(SomeClass)img.getMetadata();
您还没有声明Mockito在调用此方法时应该返回什么(使用when(..).thenReturn(..)语法),因此Mockito返回默认的null值。
当您拨打:
data.dumpData();
你得到一个NullPointerException
,因为data
就是null
。
修复
- 声明
when(img.getMetadata()).thenReturn(..)
应返回的内容 - 使用Mockito.spy而不是mock