我的单元类中有以下行。
@Reference
private ConfigService configService;
所以我试图模拟它如下,但是当这一行执行时,它会给出数组索引出界异常
whenNew(ConfigService.class).withAnyArguments().thenReturn(configService);
java.lang.ArrayIndexOutOfBoundsException: 0
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withAnyArguments(DefaultConstructorExpectationSetup.java:64)
at com.google.core.servlets.forms.HmeServletTest.testDoPost(HmeServletTest.java:82)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:66)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:94)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:84)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:118)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:101)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:53)
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:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
你必须使用这样的东西:
whenNew(ConfigService.class).withAnyArguments().thenReturn(configService);
当您必须显式调用对象的构造函数时,而不是当您使用 Injection
时。
假设MyClass
包含行
@Resource
private ConfigService service;
在你的Junit中试试这个:
@RunWith(PowerMockRunner.class)
public class Test{
@InjectMocks
private MyClass myClass;
@Mock
private ConfigService service;
@Before
public void setUp(){
myClass = new MyClass();
MockitoAnnotations.initMocks(this); // this row inits the mock of ConfiService
// and injects it in myClass
}
}
最后但并非最不重要的一点是:如果您不打算将PowerMockito
用于其他任何内容(静态或私有方法/字段/其他构造函数),则根本不能使用它。
我遇到了这个问题,经过长时间的调查,我发现我在一种方法中两次使用相同的模拟。所以我在两个 When() 声明之间使用 Mockito.reset() 休息模拟,它解决了这个问题。
例:
when( mockedQuery.getSingleResult()).thenThrow(NoResultException.class);
//some code
Mockito.reset( mockedOracleQuery );
when(mockedQuery.getSingleResult()).thenReturn(stg);
尝试使用 .withArguments()
调用。 我以前没有见过.withAnyArguments()
调用,这可能是对数组的抱怨。 在.withArguments()
调用中,您可以使用模拟any
类,因此无需指定确切的对象。
例如,你可以让一个构造函数通常采用一个String
,然后自定义类X
: whenNew(A.class).withArguments(Mockito.anyString(), Mockito.any(X.class)).thenReturn(aMock);