更详细的资料,可以查看Jmockit中文网:
http://jmockit.cn/showArticle.htm?channel=3&id=2

核心要点

MockUp方法、Expectations方法。
注意:
// native, private方法无法用Expectations来Mock

示例代码

PG

  1. package com.main.wy;
  2. public class HelloWorld {
  3. // 普通方法
  4. public String returnHello() {
  5. return "hello";
  6. }
  7. // 普通静态方法
  8. public static String staticReturnHello() {
  9. return "staticHello";
  10. }
  11. // 私有方法
  12. private String returnHelloTwo() {
  13. return "helloTwo";
  14. }
  15. // 私有静态方法
  16. private static String staticReturnHelloTwo() {
  17. return "staticHelloTwo";
  18. }
  19. }

PT

  1. package com.test.wy;
  2. import static org.junit.Assert.assertEquals;
  3. import java.lang.reflect.Method;
  4. import org.junit.Test;
  5. import com.main.wy.HelloWorld;
  6. import mockit.Expectations;
  7. import mockit.Mock;
  8. import mockit.MockUp;
  9. import mockit.Tested;
  10. public class HelloWorldTest {
  11. // 生成测试对象的实例。
  12. @Tested
  13. public HelloWorld target;
  14. @Test
  15. public void testReturnHello001() {
  16. assertEquals("hello", target.returnHello());
  17. }
  18. @Test
  19. public void testReturnHello002() {
  20. new Expectations(HelloWorld.class) {
  21. {
  22. target.returnHello();
  23. result = "1";
  24. }
  25. };
  26. assertEquals("1", target.returnHello());
  27. }
  28. @Test
  29. public void testReturnHello003() {
  30. new MockUp<HelloWorld>() {
  31. @Mock
  32. public String returnHello() {
  33. return "1";
  34. }
  35. };
  36. assertEquals("1", target.returnHello());
  37. }
  38. @Test
  39. public void testStaticReturnHello001() {
  40. assertEquals("staticHello", HelloWorld.staticReturnHello());
  41. }
  42. @Test
  43. public void testStaticReturnHello002() {
  44. new Expectations(HelloWorld.class) {
  45. {
  46. HelloWorld.staticReturnHello();
  47. result = "1";
  48. }
  49. };
  50. assertEquals("1", HelloWorld.staticReturnHello());
  51. }
  52. @Test
  53. public void testStaticReturnHello003() {
  54. new MockUp<HelloWorld>() {
  55. @Mock
  56. public String staticReturnHello() {
  57. return "1";
  58. }
  59. };
  60. assertEquals("1", HelloWorld.staticReturnHello());
  61. }
  62. @Test
  63. public void testReturnHelloTwo001() {
  64. try {
  65. // getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
  66. Method method = target.getClass().getDeclaredMethod("returnHelloTwo");
  67. // 压制Java对访问修饰符的检查
  68. method.setAccessible(true);
  69. // 调用方法;target为所在对象
  70. String result = (String) method.invoke(target, null);
  71. assertEquals("helloTwo", result);
  72. } catch (Exception e) {
  73. e.printStackTrace();
  74. }
  75. }
  76. @Test
  77. public void testReturnHelloTwo002() {
  78. try {
  79. new MockUp<HelloWorld>() {
  80. @Mock
  81. private String returnHelloTwo() {
  82. return "1";
  83. }
  84. };
  85. // getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
  86. Method method = target.getClass().getDeclaredMethod("returnHelloTwo");
  87. // 压制Java对访问修饰符的检查
  88. method.setAccessible(true);
  89. // 调用方法;target为所在对象
  90. String result = (String) method.invoke(target, null);
  91. assertEquals("1", result);
  92. } catch (Exception e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. @Test
  97. public void testStaticReturnHelloTwo001() {
  98. try {
  99. // getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
  100. Method method = target.getClass().getDeclaredMethod("staticReturnHelloTwo");
  101. // 压制Java对访问修饰符的检查
  102. method.setAccessible(true);
  103. // 调用方法;target为所在对象
  104. String result = (String) method.invoke(target, null);
  105. assertEquals("staticHelloTwo", result);
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. @Test
  111. public void testStaticReturnHelloTwo002() {
  112. try {
  113. new MockUp<HelloWorld>() {
  114. @Mock
  115. private String staticReturnHelloTwo() {
  116. return "1";
  117. }
  118. };
  119. // getDeclaredMethod可以获取到所有方法,而getMethod只能获取public
  120. Method method = target.getClass().getDeclaredMethod("staticReturnHelloTwo");
  121. // 压制Java对访问修饰符的检查
  122. method.setAccessible(true);
  123. // 调用方法;target为所在对象
  124. String result = (String) method.invoke(target, null);
  125. assertEquals("1", result);
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. }
  129. }
  130. }