/
InvalidUsageTest.java
150 lines (117 loc) · 4.55 KB
/
InvalidUsageTest.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.misuse;
import static org.junit.Assume.assumeFalse;
import static org.mockito.Mockito.*;
import org.junit.After;
import org.junit.Test;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.misusing.MissingMethodInvocationException;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
public class InvalidUsageTest extends TestBase {
@Mock private IMethods mock;
@Mock private IMethods mockTwo;
@After
public void resetState() {
super.resetState();
}
@Test(expected = MockitoException.class)
public void shouldRequireArgumentsWhenVerifyingNoMoreInteractions() {
verifyNoMoreInteractions();
}
@Test(expected = MockitoException.class)
public void shouldRequireArgumentsWhenVerifyingZeroInteractions() {
verifyZeroInteractions();
}
@Test(expected = MockitoException.class)
public void shouldRequireArgumentsWhenVerifyingNoInteractions() {
verifyNoInteractions();
}
@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = MockitoException.class)
public void shouldNotCreateInOrderObjectWithoutMocks() {
inOrder();
}
@Test(expected = MockitoException.class)
public void shouldNotAllowVerifyingInOrderUnfamilarMocks() {
InOrder inOrder = inOrder(mock);
inOrder.verify(mockTwo).simpleMethod();
}
@Test(expected = MissingMethodInvocationException.class)
public void shouldReportMissingMethodInvocationWhenStubbing() {
when(mock.simpleMethod())
.thenReturn("this stubbing is required to make sure Stubbable is pulled");
when("".toString()).thenReturn("x");
}
@Test(expected = MockitoException.class)
public void shouldNotAllowSettingInvalidCheckedException() throws Exception {
when(mock.simpleMethod()).thenThrow(new Exception());
}
@Test(expected = MockitoException.class)
public void shouldNotAllowSettingNullThrowable() throws Exception {
when(mock.simpleMethod()).thenThrow(new Throwable[] {null});
}
@SuppressWarnings("all")
@Test(expected = MockitoException.class)
public void shouldNotAllowSettingNullThrowableVararg() throws Exception {
when(mock.simpleMethod()).thenThrow((Throwable) null);
}
@Test(expected = MockitoException.class)
public void shouldNotAllowSettingNullConsecutiveThrowable() throws Exception {
when(mock.simpleMethod()).thenThrow(new RuntimeException(), null);
}
final class FinalClass {}
@Test(expected = MockitoException.class)
public void shouldNotAllowMockingFinalClassesIfDisabled() throws Exception {
assumeFalse(
"Inlining mock allows mocking final classes",
mock(FinalClass.class).getClass() == FinalClass.class);
}
@SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
@Test(expected = MockitoException.class)
public void shouldNotAllowMockingPrimitives() throws Exception {
mock(Integer.TYPE);
}
interface ObjectLikeInterface {
boolean equals(Object o);
String toString();
int hashCode();
}
@Test
public void shouldNotMockObjectMethodsOnInterface() throws Exception {
ObjectLikeInterface inter = mock(ObjectLikeInterface.class);
Object ignored = inter.equals(null);
ignored = inter.toString();
ignored = inter.hashCode();
verifyZeroInteractions(inter);
}
@Test
public void shouldNotMockObjectMethodsOnInterfaceVerifyNoInteractions() throws Exception {
ObjectLikeInterface inter = mock(ObjectLikeInterface.class);
Object ignored = inter.equals(null);
ignored = inter.toString();
ignored = inter.hashCode();
verifyNoInteractions(inter);
}
@Test
public void shouldNotMockObjectMethodsOnClass() throws Exception {
Object clazz = mock(ObjectLikeInterface.class);
Object ignored = clazz.equals(null);
ignored = clazz.toString();
ignored = clazz.hashCode();
verifyZeroInteractions(clazz);
}
@Test
public void shouldNotMockObjectMethodsOnClassVerifyNoInteractions() throws Exception {
Object clazz = mock(ObjectLikeInterface.class);
Object ignored = clazz.equals(null);
ignored = clazz.toString();
ignored = clazz.hashCode();
verifyNoInteractions(clazz);
}
}