[JAVA]在Junit中測試私有函式的方法(junit, private, method)
轉自:https://blog.csdn.net/szwangdf/article/details/533452
eclipse中如何寫一個測試私有方法的junit?
假設類Summer定義如下:
public class Summer{
private int methodone(String argsone){
//method code
.......
return 4;
}
}
測試如下:
public class SummerTest extends TestCase {
public void testMethodone(){
Object rightResult = xxxxx;
Summer example = new Summer.newInstance();
Method m = example .getClass().getDeclaredMethod("methodone",new Class[]{String.class});
m.setAccessible(true);
Object result = m.invoke(example ,new Object[] {new String(xxxx)});
m.setAccessible(false);
if (result.equals(rightResult)){
//your code
......
}
}
}
---------------------
作者:逐浪_一生懸命
來源:CSDN
原文:https://blog.csdn.net/szwangdf/article/details/533452
版權宣告:本文為博主原創文章,轉載請附上博文連結!