商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

在Junit中测试私有函数的方法



【IT168 技术文档】

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
         ......
      }
   }
}
1
【内容导航】
第1页: 在Junit中测试私有函数的方法
©版权所有。未经许可,不得转载。
[责任编辑:李宁]