测试包:jmockit-1.6
被测试类:
package edu.com;
public class T {
private int privateMethod(int x, int y){
return x + y;
}
}
测试类:
package edu.com;
import static org.junit.Assert.*;
import mockit.Deencapsulation;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TTest {
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPrivateMethod() {
// 新建一个对象,如果直接用T.class来invoke会报错
/**
* int actual = Deencapsulation.invoke(T.class, "privateMethod", 3, 5);
* java.lang.IllegalArgumentException: Attempted to invoke non-static method without an instance to invoke it on
*/
T t = new T();
int expected = 8;
int actual = Deencapsulation.invoke(t, "privateMethod", 3, 5);
assertEquals(expected, actual);
}
}