【发布时间】:2017-11-16 19:47:24
【问题描述】:
我试图模拟我的私有方法,但我得到了java.lang.IllegalArgumentException: object is not an instance of declaring class。下面是我的方法
private String decodeResponse(byte bresp[])
{
String spresp = null;
//
return spresp;
}
下面是我的测试课,
@PrepareForTest(MyClass.class)
@RunWith(PowerMockRunner.class)
public class MyClassTest{
@Test
public void test() throws Exception {
PowerMockito.spy(MyClass.class);
PowerMockito.doReturn("abcdefg").when(MyClass.class, "decodeResponse",Matchers.anyByte());
}
}
我在过去 3 小时内都陷入了这个问题。任何帮助都将不胜感激。
【问题讨论】:
-
你应该避免使用 Powermock(主要是因为它很慢)。您应该保护私有方法并在测试中“滥用”这种可见性
-
你不需要模拟私有方法,因为它与 tdd 无关
-
@fxrbfg 我的私有方法正在从另一个公共方法调用
-
这不是tdd的方式,需要自己理解。测试私有方法是滥用。您只需要测试公共方法。
-
@fxrbfg 是的,我同意。但是在这种情况下,这个特殊的私有方法是从另一个公共方法调用的。而且在这个私有方法中,我的数据库连接也是如此。
标签: java unit-testing junit powermockito