我正在测试下面的代码,但是测试用例失败了。第23行给出了InvocationTargetException。如果调用了某个其他mock的方法,返回mock是否有问题?
此外,即使我注解了第23行,下面的第26行也会使verify
失败。打开调试器会抛出InvocationTargetException。我只是检查fetchSideThing
方法是否被调用过一次。我该怎么做呢?
@Getter
@Setter
public class SideThing {
int weight;
SideThing(int weight) {
this.weight = weight;
}
public static SideThing get(int w) {
return new SideThing(w);
}
}
public class ThingGrandParent {
int tgpVal;
ThingGrandParent(int val) {
tgpVal = val;
}
public ThingParent GpFun(int y) {
return new ThingParent(y);
}
}
public class ThingParent {
SideThing st;
ThingParent(int x) {
st = new SideThing(x);
}
public SideThing fetchSideThing() {
return st;
}
private int anotherFun(int y) {
return y;
}
}
@Getter
@Setter
public class Thing {
private int size;
private int name;
Thing() {}
int bLogic(int size) throws Exception {
ThingGrandParent tgp = fun(size);
SideThing st;
ThingParent tp = tgp.GpFun(size);
st = tp.fetchSideThing();
return st.getWeight();
}
public ThingGrandParent fun(int size) {
return new ThingGrandParent(size);
}
}
@RunWith(PowerMockRunner.class)
@PowerMockIgnore("javax.management.*")
@PrepareForTest({Thing.class, SideThing.class})
public class ThingTest {
@Mock
ThingParent tp;
@Mock
ThingGrandParent tgpp;
Thing t;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
t = spy(new Thing()); // does it matter here if `t` is an InjectMocks or a spy?
}
@Test
public void test1() throws Exception {
when(tgpp.GpFun(anyInt())).thenReturn(tp); // -- line23, why InvocationTargetException?
assertEquals(4, t.bLogic(4));
verify(tp, times(1)).fetchSideThing(); //-- line26 InvocationTargetException
}
}
此外,如代码中所示,如果我对Thing t
使用spy或InjectMocks,真的会有什么不同吗?
我已经在setup()
中使用了MockitoAnnotations.initMocks(this);
,但是它说'initMocks(java.lang.Object)'已经过时了。那么我还应该在这里使用什么呢?
以下是失败消息:
Wanted but not invoked:
tp.fetchSideThing();
-> at com.amazon.configurationeditor.store.ThingTest.test1(ThingTest.java:62)
Actually, there were zero interactions with this mock.
Wanted but not invoked:
tp.fetchSideThing();
-> at com.amazon.configurationeditor.store.ThingTest.test1(ThingTest.java:62)
Actually, there were zero interactions with this mock.
at com.amazon.configurationeditor.store.ThingTest.test1(ThingTest.java:62)
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:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
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:298)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
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:136)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
1条答案
按热度按时间gudnpqoy1#
initMocks不应该从你的测试中调用。不确定你为什么要使用PowerMockito,我假设Mockito是通常的选择。删除ThingTest类上的3个注解。
如果您使用JUnit 4,请查看@Rule和Mockito.rule(),例如。
编辑:我假设还有一些其他的关键问题,Thing 't'没有tp或tgpp集,它将创建新的对象,这些不是mock。
确保Thing t为spy进行了正确的初始化。也许你应该在设置中添加:一个月一个月?