Java Mockito和PowerMock:需要但未调用
我知道其他人问过这个(或类似)问题,但没有一个解决方案对我有帮助。我有三门课:Java Mockito和PowerMock:需要但未调用,java,unit-testing,mockito,powermock,powermockito,Java,Unit Testing,Mockito,Powermock,Powermockito,我知道其他人问过这个(或类似)问题,但没有一个解决方案对我有帮助。我有三门课: public class A { public int superMethod(int val) { B b = new B(); return b.subMethod(val); } } 我的测试班: public class MockitoTest { @Mock B b; @InjectMocks A a;
public class A {
public int superMethod(int val) {
B b = new B();
return b.subMethod(val);
}
}
我的测试班:
public class MockitoTest {
@Mock
B b;
@InjectMocks
A a;
@Test
public void testMethodA(){
a = mock(A.class);
doCallRealMethod().when(a).superMethod(anyInt());
b = mock(B.class);
try {
whenNew(B.class).withAnyArguments().thenReturn(b);
} catch (Exception e) {}
a.superMethod(5);
verify(b).subMethod(anyInt());
}
}
我想验证在A.superMethod()中是否调用了B.subMethod()。我怎样才能做到这一点。
我知道我需要为此使用PowerMock,但我不确定如何使用。
此外,我不允许更改关于A类或B类的任何内容
任何帮助都将不胜感激 我的问题在于我的注释(或缺少注释)。我在MockitoTest类上缺少@PrepareForTest({A.class})注释。您可能希望在该
catch
块中打印堆栈跟踪,以防引发忽略的异常。只需使用e.printStackTrace()进行尝试。没有引发任何异常。
public class MockitoTest {
@Mock
B b;
@InjectMocks
A a;
@Test
public void testMethodA(){
a = mock(A.class);
doCallRealMethod().when(a).superMethod(anyInt());
b = mock(B.class);
try {
whenNew(B.class).withAnyArguments().thenReturn(b);
} catch (Exception e) {}
a.superMethod(5);
verify(b).subMethod(anyInt());
}
}