终极测试代码
1 package com.khlin.test.junit.jmockit.demo; 2 3 import mockit.Deencapsulation; 4 import mockit.Expectations; 5 import mockit.Injectable; 6 import mockit.Mock; 7 import mockit.MockUp; 8 import mockit.Mocked; 9 import mockit.NonStrictExpectations; 10 import mockit.Tested; 11 import mockit.Verifications; 12 import mockit.integration.junit4.JMockit; 13 14 import org.junit.Assert; 15 import org.junit.Test; 16 import org.junit.runner.RunWith; 17 18 import com.khlin.test.junit.jmockit.demo.jmockit.AbstractA; 19 import com.khlin.test.junit.jmockit.demo.jmockit.ClassA; 20 import com.khlin.test.junit.jmockit.demo.jmockit.ClassB; 21 import com.khlin.test.junit.jmockit.demo.jmockit.InterfaceB; 22 23 @RunWith(JMockit.class) 24 public class JMockitTest { 25 26 /** 27 * mock私有方法 28 */ 29 @Test 30 public void testPrivateMethod() { 31 32 final ClassA a = new ClassA(); 33 // 局部参数,把a传进去 34 new NonStrictExpectations(a) { 35 { 36 Deencapsulation.invoke(a, "getAnythingPrivate"); 37 result = 100; 38 times = 1; 39 } 40 }; 41 42 Assert.assertEquals(100, a.getAnything()); 43 44 new Verifications() { 45 { 46 Deencapsulation.invoke(a, "getAnythingPrivate"); 47 times = 1; 48 } 49 }; 50 } 51 52 /** 53 * mock私有静态方法 54 */ 55 @Test 56 public void testPrivateStaticMethod() { 57 58 new NonStrictExpectations(ClassA.class) { 59 { 60 Deencapsulation 61 .invoke(ClassA.class, "getStaticAnythingPrivate"); 62 result = 100; 63 times = 1; 64 } 65 }; 66 67 Assert.assertEquals(100, ClassA.getStaticAnything()); 68 69 new Verifications() { 70 { 71 Deencapsulation 72 .invoke(ClassA.class, "getStaticAnythingPrivate"); 73 times = 1; 74 } 75 }; 76 77 } 78 79 /** 80 * mock公有方法 81 */ 82 @Test 83 public void testPublicMethod() { 84 final ClassA classA = new ClassA(); 85 new NonStrictExpectations(classA) { 86 { 87 classA.getAnything(); 88 result = 100; 89 times = 1; 90 } 91 }; 92 93 Assert.assertEquals(100, classA.getAnything()); 94 95 new Verifications() { 96 { 97 classA.getAnything(); 98 times = 1; 99 } 100 }; 101 } 102 103 /** 104 * mock公有静态方法--基于行为 105 */ 106 @Test 107 public void testPublicStaticMethod() { 108 109 new NonStrictExpectations(ClassA.class) { 110 { 111 ClassA.getStaticAnything(); 112 result = 100; 113 times = 1; 114 } 115 }; 116 117 Assert.assertEquals(100, ClassA.getStaticAnything()); 118 119 new Verifications() { 120 { 121 ClassA.getStaticAnything(); 122 times = 1; 123 } 124 }; 125 } 126 127 /** 128 * mock公有静态方法--基于状态 129 */ 130 @Test 131 public void testPublicStaticMethodBaseOnStatus() { 132 133 MockUp<ClassA> mockUp = new MockUp<ClassA>() { 134 @Mock 135 public int getStaticAnything() { //注意这里不用声明为static 136 return 100; 137 } 138 }; 139 140 Assert.assertEquals(100, ClassA.getStaticAnything()); 141 } 142 143 /** 144 * mock接口 145 */ 146 @Test 147 public void testInterface() { 148 149 InterfaceB interfaceB = new MockUp<InterfaceB>() { 150 @Mock 151 public int getAnything() { 152 return 100; 153 } 154 }.getMockInstance(); 155 156 157 ClassA classA = new ClassA(); 158 classA.setInterfaceB(interfaceB); 159 160 Assert.assertEquals(100, classA.getClassBAnything()); 161 } 162 163 /** 164 * mock接口--基于状态 165 */ 166 @Test 167 public void testInterfaceBasedOnStatus() { 168 final InterfaceB interfaceB = new ClassB(); 169 170 new NonStrictExpectations(interfaceB) { 171 { 172 interfaceB.getAnything(); 173 result = 100; 174 times = 1; 175 } 176 }; 177 178 ClassA classA = new ClassA(); 179 classA.setInterfaceB(interfaceB); 180 181 Assert.assertEquals(100, classA.getClassBAnything()); 182 183 new Verifications() { 184 { 185 interfaceB.getAnything(); 186 times = 1; 187 } 188 }; 189 } 190 191 192 193 /** 194 * mock抽象类 195 */ 196 @Test 197 public void testAbstract() { 198 AbstractA abstractA = new MockUp<AbstractA>() { 199 @Mock 200 public int getAbstractAnything(){ 201 return 100; 202 } 203 204 @Mock 205 public int getAnything(){ 206 return 1000; 207 } 208 }.getMockInstance(); 209 210 Assert.assertEquals(100, abstractA.getAbstractAnything()); 211 212 Assert.assertEquals(1000, abstractA.getAnything()); 213 } 214 } |