Posted on 2007-10-23 11:11
G_G 閱讀(2277)
評論(1) 編輯 收藏 所屬分類:
JUnit
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
?????????......
??????}
???}
}
