无法使用选项CALLS_REAL_METHODS模拟ArrayList

时间:2016-05-19 17:15:02

标签: java junit mockito

根据官方文件:http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#CALLS_REAL_METHODS

我写了以下测试:

package tk.puncha.study;

import org.junit.Test;

import java.util.ArrayList;

import static org.mockito.Mockito.*;

public class MockitoTest {

  @Test
  public void test() {
    ArrayList arrayList = mock(ArrayList.class, withSettings().defaultAnswer((CALLS_REAL_METHODS)));
    arrayList.add(new Object());
  }
}

但它崩溃时出现以下异常:

java.lang.NullPointerException
at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:234)
at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
at java.util.ArrayList.add(ArrayList.java:458)
at tk.puncha.study.MockitoTest.test(MockitoTest.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
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.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.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

这是一个JUnit错误还是我错了?

2 个答案:

答案 0 :(得分:0)

使用CALLS_REAL_METHODS默认答案的模拟dangerous。 Mockito并不会将您的模拟对象的字段初始化为逼真(非空),因此真实方法会与无效字段进行交互并导致错误,例如您正在查看的字段。

此处,ArrayList.ensureExplicitCapacity accesses elementData.length必须是null elementData数组,这会导致您的错误。

如果您使用的是spy,而默认为CALLS_REAL_METHODS行为,则Mockito会复制您提供的实际构造的对象实例中的字段值。这通常更安全。

(旁注:除了玩具示例之外,请记住,模拟您不拥有的类型是危险的,并且几乎从来没有理由去模拟经过良好测试的简单库对象像List或其实现。)

答案 1 :(得分:0)

试试这个: ArrayList arrayList = mock(ArrayList.class,withSettings()。useConstructor()。defaultAnswer((CALLS_REAL_METHODS)));