<dependency> <groupId>org.easymock</groupId> <artifactId>easymock</artifactId> <version>3.1</version> <scope>test</scope> </dependency> |
public class Class1Mocked { public String hello(String name){ System.out.println("hello "+name); return "hello "+name; } public void show(){ System.out.println("Class1Mocked.show()"); } } |
@Test public void testMockMethod() { Class1Mocked obj = createMock(Class1Mocked.class);① expect(obj.hello("z3")).andReturn("hello l4");② replay(obj);③ String actual = obj.hello("z3");④ assertEquals("hello l4", actual); verify(obj);⑤ } |
<dependency> <groupId>org.mockito</groupId> <artifactId>mockito-all</artifactId> <version>1.9.5</version> <scope>test</scope> </dependency> |
@Test public void testMockMethod() { Class1Mocked obj=mock(Class1Mocked.class);① when(obj.hello("z3")).thenReturn("hello l4");② String actual=obj.hello("z3");③ assertEquals("hello l4",actual); verify(obj).hello("z3");④ //verify(obj,times(1)).hello("z3"); //能夠加參數驗證次數 } |
@Test public void testMockMethodInOrder() { Class1Mocked objOther = mock(Class1Mocked.class); Class1Mocked objCn = mock(Class1Mocked.class); when(objOther.hello("z3")).thenReturn("hello l4"); when(objCn.hello("z3")).thenReturn("hello 張三"); String other = objOther.hello("z3"); assertEquals("hello l4", other); String cn = objCn.hello("z3"); assertEquals("hello 張三", cn); InOrder inOrder = inOrder(objOther, objCn); //此行並不決定順序,下面的兩行纔開始驗證順序 inOrder.verify(objOther).hello("z3"); inOrder.verify(objCn).hello("z3"); } |
@Test public void testSkipExpect() { Class1Mocked obj = mock(Class1Mocked.class); assertEquals(null, obj.hello("z3")); obj.show(); verify(obj).hello("z3"); verify(obj).show(); } |
@Test public void testCallRealMethod () { Class1Mocked obj = mock(Class1Mocked.class); doCallRealMethod().when(obj).hello("z3"); assertEquals("hello z3",obj.hello("z3")); assertEquals(null,obj.hello("l4")); obj.show(); verify(obj).hello("z3"); verify(obj).hello("l4"); verify(obj).show(); } |
@Test public void testSpy() { Class1Mocked obj = spy(new Class1Mocked()); doNothing().when(obj).show(); assertEquals("hello z3",obj.hello("z3")); obj.show(); verify(obj).hello("z3"); verify(obj).show(); } |
@Test public void testSpy2() { Class1Mocked obj = spy(new Class1Mocked()); when(obj.hello("z3")).thenReturn("hello l4"); assertEquals("hello l4",obj.hello("z3")); verify(obj).hello("z3"); } |
@Test public void testSpy3() { Class1Mocked obj = spy(new Class1Mocked()); doReturn("hello l4").when(obj).hello("z3"); assertEquals("hello l4",obj.hello("z3")); verify(obj).hello("z3"); } |
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.5</version> <scope>test</scope> </dependency> <dependency> <groupId>org.powermock</groupId> <artifactId>powermock-module-junit4</artifactId> <version>1.5</version> <scope>test</scope> </dependency> |
public class Class2Mocked { public static int getDouble(int i){ return i*2; } public String getTripleString(int i){ return multiply3(i)+""; } private int multiply3(int i){ return i*3; } } |
@Test public void testMockStaticMethod() { mockStatic(Class2Mocked.class); when(Class2Mocked.getDouble(1)).thenReturn(3); int actual = Class2Mocked.getDouble(1); assertEquals(3, actual); verifyStatic(); Class2Mocked.getDouble(1); } |
@Test public void testMockPrivateMethod() throws Exception { Class2Mocked obj = mock(Class2Mocked.class); when(obj, "multiply3", 1).thenReturn(4); doCallRealMethod().when(obj).getTripleString(1); String actual = obj.getTripleString(1); assertEquals("4", actual); verifyPrivate(obj).invoke("multiply3", 1); } |
@Test public void testMockPrivateMethod2() throws Exception { Class2Mocked obj = spy(new Class2Mocked()); when(obj, "multiply3", 1).thenReturn(4); String actual = obj.getTripleString(1); assertEquals("4", actual); verifyPrivate(obj).invoke("multiply3", 1); } |
@Test public void testStructureWhenPathDoesntExist() throws Exception { final String directoryPath = "mocked path"; File directoryMock = mock(File.class); whenNew(File.class).withArguments(directoryPath).thenReturn(directoryMock); when(directoryMock.exists()).thenReturn(true); File file=new File(directoryPath); assertTrue(file.exists()); verifyNew(File.class).withArguments(directoryPath); verifyPrivate(directoryMock).invoke("exists"); } |
<dependency> <groupId>com.googlecode.jmockit</groupId> <artifactId>jmockit</artifactId> <version>1.0</version> <scope>test</scope> </dependency> |
@Mocked //用@Mocked標註的對象,不須要賦值,jmockit自動mock Class1Mocked obj;@Test public void testMockNormalMethod1() { new Expectations() { { obj.hello("z3"); returns("hello l4", "hello w5"); obj.hello("張三"); result="hello 李四"; } }; assertEquals("hello l4", obj.hello("z3")); assertEquals("hello w5", obj.hello("z3")); assertEquals("hello 李四", obj.hello("張三")); try { obj.hello("z3"); } catch (Throwable e) { System.out.println("第三次調用hello(\"z3\")會拋出異常"); } try { obj.show(); } catch (Throwable e) { System.out.println("調用沒有在Expectations塊中定義的方法show()會拋出異常"); } } |
public void testMockNormalMethod2() { new NonStrictExpectations() { { obj.hello("z3"); returns("hello l4", "hello w5"); } }; assertEquals("hello l4", obj.hello("z3")); assertEquals("hello w5", obj.hello("z3")); assertEquals("hello w5", obj.hello("z3"));// 會返回在NonStrictExpectations塊中定義的最後一個返回值 obj.show(); new Verifications() { { obj.hello("z3"); times = 3; obj.show(); times = 1; } }; } |
@Test public void testMockNormalMethod() throws IOException { final Class1Mocked obj = new Class1Mocked();//也能夠不用@Mocked標註,但須要final關鍵字 new NonStrictExpectations(obj) { { obj.hello("z3"); result = "hello l4"; } }; assertEquals("hello l4", obj.hello("z3")); assertEquals("hello 張三", obj.hello("張三")); new Verifications() { { obj.hello("z3"); times = 1; obj.hello("張三"); times = 1; } }; } |
@Test public void testMockStaticMethod() { new NonStrictExpectations(Class2Mocked.class) { { Class2Mocked.getDouble(1); result = 3; } }; assertEquals(3, Class2Mocked.getDouble(1)); new Verifications() { { Class2Mocked.getDouble(1); times = 1; } }; } |
@Test public void testMockPrivateMethod() throws Exception { final Class2Mocked obj = new Class2Mocked(); new NonStrictExpectations(obj) { { this.invoke(obj, "multiply3", 1); result = 4; } }; String actual = obj.getTripleString(1); assertEquals("4", actual); new Verifications() { { this.invoke(obj, "multiply3", 1); times = 1; } }; } |
public class Class3Mocked { private String name = "name_init"; public String getName() { return name; } private static String className="Class3Mocked_init"; public static String getClassName(){ return className; } public static int getDouble(int i){ return i*2; } public int getTriple(int i){ return i*3; } } |
@Test public void testMockPrivateProperty() throws IOException { final Class3Mocked obj = new Class3Mocked(); new NonStrictExpectations(obj) { { this.setField(obj, "name", "name has bean change!"); } }; assertEquals("name has bean change!", obj.getName()); } |
@Test public void testMockPrivateStaticProperty() throws IOException { new NonStrictExpectations(Class3Mocked.class) { { this.setField(Class3Mocked.class, "className", "className has bean change!"); } }; assertEquals("className has bean change!", Class3Mocked.getClassName()); } |
@Test public void testMockNormalMethodContent() throws IOException { final Class3Mocked obj = new Class3Mocked(); new NonStrictExpectations(obj) { { new MockUp<Class3Mocked>() { @Mock public int getTriple(int i) { return i * 30; } }; } }; assertEquals(30, obj.getTriple(1)); assertEquals(60, obj.getTriple(2)); } |
public class Class4Mocked { @Mock public static int getDouble(int i){ return i*20; } } |
@Testpublic void testDynamicMockStaticMethodContent() throws IOException { Mockit.setUpMock(Class3Mocked.class, Class4Mocked.class); assertEquals(20, Class3Mocked.getDouble(1)); assertEquals(40, Class3Mocked.getDouble(2));} |