-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTrackInvocationTest.java
More file actions
70 lines (54 loc) · 2.33 KB
/
TrackInvocationTest.java
File metadata and controls
70 lines (54 loc) · 2.33 KB
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
package io.github.walterinsh.mock;
import org.testng.annotations.Test;
import static org.mockito.Mockito.*;
/**
* Usually, we want to know more about our mocked objects. The number of how many times our object has been invoked is
* important under some circumstances. It helps us to know what really happened underlying.
* Created by Walter on 12/2/15.
*/
public class TrackInvocationTest {
/**
* With Mockito.verify() and some special asserts(times, atLeast, atMost, never), we can reinforce our unit tests.
* @throws Exception
*/
@Test
public void trackInvocationTimes() throws Exception {
TargetClass targetClass = mock(TargetClass.class);
targetClass.targetMethod(null);
targetClass.targetMethod("A");
targetClass.targetMethod("A");
targetClass.targetMethod("B");
verify(targetClass, times(2)).targetMethod("A");
verify(targetClass, times(1)).targetMethod("B");
verify(targetClass, times(4)).targetMethod(anyString());
verify(targetClass, atLeast(2)).targetMethod("A");
verify(targetClass, atLeastOnce()).targetMethod("B");
verify(targetClass, atMost(1)).targetMethod("B");
verify(targetClass, never()).targetMethod("C");
}
/**
* If you want to ensure there are no more interactions(invocations) except what you need.
* Mockito.verifyNoMoreInteractions might help.
*
* @throws Exception
*/
@Test
public void noMoreInvocations() throws Exception {
TargetClass targetClass = mock(TargetClass.class);
// one interaction should happen
targetClass.targetMethod(null);
/*
The next commented code should not have happened. If you uncomment the code, the test fails,and with this
friendly information
For your reference, here is the list of all invocations ([?] - means unverified).
1. -> at io.github.walterinsh.mock.TrackInvocation.noMoreInvocations(TrackInvocation.java:50)
2. [?]-> at io.github.walterinsh.mock.TrackInvocation.noMoreInvocations(TrackInvocation.java:60)
*/
// targetClass.targetMethod("");
verify(targetClass, times(1)).targetMethod(null);
verifyNoMoreInteractions(targetClass);
}
class TargetClass {
public void targetMethod(String param) {}
}
}