【发布时间】:2023-03-06 11:48:01
【问题描述】:
我需要编写一个测试来验证当私有method_C抛出IOException时,Method_B返回True。 但是
public final class A{
public static Boolean Method_B(){
try{
//call a private method C which throws IOException
Method_C
}
catch(final IOException e) {
return Boolean.True
}
}
private static Method_C() throws IOException {
return something;
}
我尝试了什么:
@Test
public void testSomeExceptionOccured() throws IOException {
A Amock = mock(A.class);
doThrow(IOException.class).when(Amock.Method_C(any(),any(),any(),any()));
Boolean x = A.Method_B(some_inputs);
Assert.assertEquals(Boolean.TRUE, x);
}
我收到编译错误: 1.不能模拟最后一堂课 2. Method_C在A中有私有访问权限
关于如何纠正此问题的任何建议?
【问题讨论】:
-
测试公共接口,而不是私有实现。引发异常的条件是什么?更改公共方法输入和/或(模拟的)依赖项以引发异常。
-
@BeUndead 是正确的,还有这个:stackoverflow.com/questions/21105403/…
标签: java unit-testing junit mockito powermockito