The class contain private method need to be test.
public class TestPrivateMethod {
private int add(int a, int b)
{
return a+b;
}
}
The junit for private method test.
public class TestPrivateMethodAdd {
@Test
public void test() {
TestPrivateMethod testPrivateMethod = new TestPrivateMethod();
try {
Method method = testPrivateMethod.getClass().getDeclaredMethod(
"add", new Class[] { Integer.TYPE, Integer.TYPE });
method.setAccessible(true);
Object rtn = method.invoke(testPrivateMethod, new Object[] { 10, 1 });
assertEquals(11, rtn);
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}