更详细的资料,可以查看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 {// 生成测试对象的实例。@Testedpublic HelloWorld target;@Testpublic void testReturnHello001() {assertEquals("hello", target.returnHello());}@Testpublic void testReturnHello002() {new Expectations(HelloWorld.class) {{target.returnHello();result = "1";}};assertEquals("1", target.returnHello());}@Testpublic void testReturnHello003() {new MockUp<HelloWorld>() {@Mockpublic String returnHello() {return "1";}};assertEquals("1", target.returnHello());}@Testpublic void testStaticReturnHello001() {assertEquals("staticHello", HelloWorld.staticReturnHello());}@Testpublic void testStaticReturnHello002() {new Expectations(HelloWorld.class) {{HelloWorld.staticReturnHello();result = "1";}};assertEquals("1", HelloWorld.staticReturnHello());}@Testpublic void testStaticReturnHello003() {new MockUp<HelloWorld>() {@Mockpublic String staticReturnHello() {return "1";}};assertEquals("1", HelloWorld.staticReturnHello());}@Testpublic void testReturnHelloTwo001() {try {// getDeclaredMethod可以获取到所有方法,而getMethod只能获取publicMethod 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();}}@Testpublic void testReturnHelloTwo002() {try {new MockUp<HelloWorld>() {@Mockprivate String returnHelloTwo() {return "1";}};// getDeclaredMethod可以获取到所有方法,而getMethod只能获取publicMethod 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();}}@Testpublic void testStaticReturnHelloTwo001() {try {// getDeclaredMethod可以获取到所有方法,而getMethod只能获取publicMethod 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();}}@Testpublic void testStaticReturnHelloTwo002() {try {new MockUp<HelloWorld>() {@Mockprivate String staticReturnHelloTwo() {return "1";}};// getDeclaredMethod可以获取到所有方法,而getMethod只能获取publicMethod 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();}}}
