关于junit测试内部类问题
有如下类:
public class InnerClass {
public void print() {
System.out.println( "Hello World ");
}
private class Inner {
private Inner() {
}
public void printe() {
System.out.println( "--------- ");
}
}
}
请问如何测试InnerClass$Inner?
我的初步想法如下,可是没有成功:
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import junit.framework.TestCase;
public class InnerClassTest extends TestCase {
public void testPrint() {
new InnerClass().print();
}
public void testInnerClass() {
InnerClass iner = new InnerClass();
Class inerClass = iner.getClass();
Class[] in = inerClass.getDeclaredClasses();
for (Class c : in) {
System.out.println(c.getName());
}
Class clIner = in[0];
try {
Constructor constructor = clIner.getDeclaredConstructor();//这里会出错的,
constructor.setAccessible(true);
Method method = clIner.getMethod( "printe ", new Class[] {});
method.setAccessible(true);
constructor.newInstance(new Object[] {});
method.invoke(constructor.newInstance(new Object[] {}),
new Object[] {});
} catch (Exception e) {
e.printStackTrace();
}
}