[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 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_36688928/article/details/84337765