Java中的Mock测试框架Mockito

上周无意中了解到一个很有意思的Java mock测试套件Mockito,简单研究了下,感觉拿来写单元测试和集成测试应该很好用,而且用起来也很方便,话不多说,直接看代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.mockedtesting;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import org.junit.Test;

public class MockedObjectUnitTest {

@Test
public void testMock() {
List mockedList = mock(List.class);

mockedList.add("one");
mockedList.add("two");
mockedList.add("three times");
mockedList.add("three times");
mockedList.add("three times");
when(mockedList.size()).thenReturn(100);
assertEquals(mockedList.size(), 100);

// as you see, load and clear
verify(mockedList, atLeastOnce()).add("one");
verify(mockedList, times(1)).add("two");
verify(mockedList, times(3)).add("three times");
verify(mockedList, never()).isEmpty();
}

@Test
public void createMockObject() {
List mockedList = mock(List.class);
assertTrue(mockedList instanceof List);

ArrayList mockedArrayList = mock(ArrayList.class);
assertTrue(mockedArrayList instanceof List);
assertTrue(mockedArrayList instanceof ArrayList);
}

@Test
public void configMockObject() {
List mockedList = mock(List.class);

// CONFIG: when calling mockedList.add("one"), return true
when(mockedList.add("one")).thenReturn(true);

// CONFIG: when calling mockedList.size(), return 1
when(mockedList.size()).thenReturn(1);

assertTrue(mockedList.add("one"));
// Because there's no config for mockedList.add("two"), false is
// returned as default value
assertFalse(mockedList.add("two"));
assertEquals(mockedList.size(), 1);

Iterator i = mock(Iterator.class);

// CONFIG: when calling i.next(), return "Hello," at 1st time,
// return "Mockito" at 2nd time
when(i.next()).thenReturn("Hello,").thenReturn("Mockito!");
String result = i.next() + " " + i.next();
assertEquals("Hello, Mockito!", result);
}

@Test(expected = NoSuchElementException.class)
public void testForException() throws Exception {
Iterator i = mock(Iterator.class);
when(i.next()).thenReturn("Hello,").thenReturn("Mockito!");
String result = i.next() + " " + i.next();
assertEquals("Hello, Mockito!", result);

// CONFIG: throw exception when calling i.next() for the 3rd time,
// which means only 2 elements in i.
doThrow(new NoSuchElementException()).when(i).next();
i.next(); // now it is the 3rd time to call i.next()
}
}