更详细的资料,可以查看Jmockit中文网:
http://jmockit.cn/showArticle.htm?channel=3&id=2
核心要点
MockUp方法、Expectations方法。
注意:// native, private方法无法用Expectations来Mock
示例代码
PG
package com.main.wy;
public class HelloWorld {
// 普通方法
public String returnHello() {
return "hello";
}
// 普通静态方法
public static String staticReturnHello() {
return "staticHello";
}
// 私有方法
private String returnHelloTwo() {
return "helloTwo";
}
// 私有静态方法
private static String staticReturnHelloTwo() {
return "staticHelloTwo";
}
}
PT
package com.test.wy;
import static org.junit.Assert.assertEquals;
import java.lang.reflect.Method;
import org.junit.Test;
import com.main.wy.HelloWorld;
import mockit.Expectations;
import mockit.Mock;
import mockit.MockUp;
import mockit.Tested;
public class HelloWorldTest {
// 生成测试对象的实例。
@Tested
public HelloWorld target;
@Test
public void testReturnHello001() {
assertEquals("hello", target.returnHello());
}
@Test
public void testReturnHello002() {
new Expectations(HelloWorld.class) {
{
target.returnHello();
result = "1";
}
};
assertEquals("1", target.returnHello());
}
@Test
public void testReturnHello003() {
new MockUp<HelloWorld>() {
@Mock
public String returnHello() {
return "1";
}
};
assertEquals("1", target.returnHello());
}
@Test
public void testStaticReturnHello001() {
assertEquals("staticHello", HelloWorld.staticReturnHello());
}
@Test
public void testStaticReturnHello002() {
new Expectations(HelloWorld.class) {
{
HelloWorld.staticReturnHello();
result = "1";
}
};
assertEquals("1", HelloWorld.staticReturnHello());
}
@Test
public void testStaticReturnHello003() {
new MockUp<HelloWorld>() {
@Mock
public String staticReturnHello() {
return "1";
}
};
assertEquals("1", HelloWorld.staticReturnHello());
}
@Test
public void testReturnHelloTwo001() {
try {
// getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
Method method = target.getClass().getDeclaredMethod("returnHelloTwo");
// 压制Java对访问修饰符的检查
method.setAccessible(true);
// 调用方法;target为所在对象
String result = (String) method.invoke(target, null);
assertEquals("helloTwo", result);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testReturnHelloTwo002() {
try {
new MockUp<HelloWorld>() {
@Mock
private String returnHelloTwo() {
return "1";
}
};
// getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
Method method = target.getClass().getDeclaredMethod("returnHelloTwo");
// 压制Java对访问修饰符的检查
method.setAccessible(true);
// 调用方法;target为所在对象
String result = (String) method.invoke(target, null);
assertEquals("1", result);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testStaticReturnHelloTwo001() {
try {
// getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
Method method = target.getClass().getDeclaredMethod("staticReturnHelloTwo");
// 压制Java对访问修饰符的检查
method.setAccessible(true);
// 调用方法;target为所在对象
String result = (String) method.invoke(target, null);
assertEquals("staticHelloTwo", result);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void testStaticReturnHelloTwo002() {
try {
new MockUp<HelloWorld>() {
@Mock
private String staticReturnHelloTwo() {
return "1";
}
};
// getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
Method method = target.getClass().getDeclaredMethod("staticReturnHelloTwo");
// 压制Java对访问修饰符的检查
method.setAccessible(true);
// 调用方法;target为所在对象
String result = (String) method.invoke(target, null);
assertEquals("1", result);
} catch (Exception e) {
e.printStackTrace();
}
}
}