-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
StubbingWithCustomAnswerTest.java
134 lines (108 loc) · 4.31 KB
/
StubbingWithCustomAnswerTest.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.stubbing;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.Set;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
public class StubbingWithCustomAnswerTest extends TestBase {
@Mock private IMethods mock;
@Test
public void shouldAnswer() throws Exception {
when(mock.simpleMethod(anyString()))
.thenAnswer(
new Answer<String>() {
public String answer(InvocationOnMock invocation) throws Throwable {
String arg = invocation.getArgument(0);
return invocation.getMethod().getName() + "-" + arg;
}
});
assertEquals("simpleMethod-test", mock.simpleMethod("test"));
}
@Test
public void shouldAnswerWithThenAnswerAlias() throws Exception {
RecordCall recordCall = new RecordCall();
Set<?> mockedSet = (Set<?>) when(mock(Set.class).isEmpty()).then(recordCall).getMock();
boolean unused = mockedSet.isEmpty();
assertTrue(recordCall.isCalled());
}
@Test
public void shouldAnswerConsecutively() throws Exception {
when(mock.simpleMethod())
.thenAnswer(
new Answer<String>() {
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getMethod().getName();
}
})
.thenReturn("Hello")
.thenAnswer(
new Answer<String>() {
public String answer(InvocationOnMock invocation) throws Throwable {
return invocation.getMethod().getName() + "-1";
}
});
assertEquals("simpleMethod", mock.simpleMethod());
assertEquals("Hello", mock.simpleMethod());
assertEquals("simpleMethod-1", mock.simpleMethod());
assertEquals("simpleMethod-1", mock.simpleMethod());
}
@Test
public void shouldAnswerVoidMethod() throws Exception {
RecordCall recordCall = new RecordCall();
doAnswer(recordCall).when(mock).voidMethod();
mock.voidMethod();
assertTrue(recordCall.isCalled());
}
@Test
public void shouldAnswerVoidMethodConsecutively() throws Exception {
RecordCall call1 = new RecordCall();
RecordCall call2 = new RecordCall();
doAnswer(call1)
.doThrow(new UnsupportedOperationException())
.doAnswer(call2)
.when(mock)
.voidMethod();
mock.voidMethod();
assertTrue(call1.isCalled());
assertFalse(call2.isCalled());
try {
mock.voidMethod();
fail();
} catch (UnsupportedOperationException e) {
}
mock.voidMethod();
assertTrue(call2.isCalled());
}
@Test
public void shouldMakeSureTheInterfaceDoesNotChange() throws Exception {
when(mock.simpleMethod(anyString()))
.thenAnswer(
new Answer<String>() {
public String answer(InvocationOnMock invocation) throws Throwable {
assertTrue(invocation.getArguments().getClass().isArray());
assertEquals(IMethods.class, invocation.getMethod().getDeclaringClass());
return "assertions passed";
}
});
assertEquals("assertions passed", mock.simpleMethod("test"));
}
private static class RecordCall implements Answer<Object> {
private boolean called = false;
public boolean isCalled() {
return called;
}
public Object answer(InvocationOnMock invocation) throws Throwable {
called = true;
return null;
}
}
}