如何测试类的静态Static方法?
a)将private方法的访问符改为 default (因为default访问修饰符课在同一个包中访问)
b) 用反射机制 method.getDeclaredMethod()
待测试类:
package com.sysu.junit3;
public class PrivateCalculator {
private int add(int a,int b)
{
return a+b;
}
}
测试类:
package com.sysu.junit3;
import java.lang.reflect.Method;
import junit.framework.Assert;
import junit.framework.TestCase;
public class PrivateCalculatorTest extends TestCase {
private PrivateCalculator calculator = null;
@Override
protected void setUp() throws Exception {
calculator = new PrivateCalculator();
}
public void testAdd() {
try {
Class<PrivateCalculator> classtype = PrivateCalculator.class;
Method method = classtype.getDeclaredMethod("add", new Cla