需要测试的方法
package com.test.junit;
public class Calucator2 {
private int add(int a,int b){
return a+b;
}
}
// 单元测试类
package com.test.junit;
import java.lang.reflect.Method;
import junit.framework.Assert;
import junit.framework.TestCase;
public class Calucator2Test extends TestCase {
public void testAdd(){
try{
Calucator2 ct2=new Calucator2();
Class<Calucator2> clazz=Calucator2.class;
Method method=clazz.getDeclaredMethod("add",new Class[]{Integer.TYPE,
Integer.TYPE});
method.setAccessible(true);
Object result=method.invoke(ct2, new Object[]{2,5});
Assert.assertEquals(7, result);
}catch(Exception e){
Assert.fail();
}
}
}