• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.misuse;
6 
7 import static org.assertj.core.api.Assertions.assertThatThrownBy;
8 import static org.mockito.Mockito.inOrder;
9 import static org.mockito.Mockito.mock;
10 import static org.mockito.Mockito.verifyNoInteractions;
11 import static org.mockito.Mockito.verifyNoMoreInteractions;
12 import static org.mockito.Mockito.when;
13 
14 import org.junit.After;
15 import org.junit.Test;
16 import org.mockito.InOrder;
17 import org.mockito.Mock;
18 import org.mockito.exceptions.base.MockitoException;
19 import org.mockito.exceptions.misusing.MissingMethodInvocationException;
20 import org.mockitousage.IMethods;
21 import org.mockitoutil.TestBase;
22 
23 public class InvalidUsageTest extends TestBase {
24 
25     @Mock private IMethods mock;
26     @Mock private IMethods mockTwo;
27 
28     @After
resetState()29     public void resetState() {
30         super.resetState();
31     }
32 
33     @Test
shouldRequireArgumentsWhenVerifyingNoMoreInteractions()34     public void shouldRequireArgumentsWhenVerifyingNoMoreInteractions() {
35         assertThatThrownBy(
36                         () -> {
37                             verifyNoMoreInteractions();
38                         })
39                 .isInstanceOf(MockitoException.class)
40                 .hasMessageContainingAll(
41                         "Method requires argument(s)!",
42                         "Pass mocks that should be verified, e.g:",
43                         "    verifyNoMoreInteractions(mockOne, mockTwo);",
44                         "    verifyNoInteractions(mockOne, mockTwo);");
45     }
46 
47     @Test
shouldRequireArgumentsWhenVerifyingNoInteractions()48     public void shouldRequireArgumentsWhenVerifyingNoInteractions() {
49         assertThatThrownBy(
50                         () -> {
51                             verifyNoInteractions();
52                         })
53                 .isInstanceOf(MockitoException.class)
54                 .hasMessageContainingAll(
55                         "Method requires argument(s)!",
56                         "Pass mocks that should be verified, e.g:",
57                         "    verifyNoMoreInteractions(mockOne, mockTwo);",
58                         "    verifyNoInteractions(mockOne, mockTwo);");
59     }
60 
61     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
62     @Test
shouldNotCreateInOrderObjectWithoutMocks()63     public void shouldNotCreateInOrderObjectWithoutMocks() {
64         assertThatThrownBy(
65                         () -> {
66                             inOrder();
67                         })
68                 .isInstanceOf(MockitoException.class)
69                 .hasMessageContainingAll(
70                         "Method requires argument(s)!",
71                         "Pass mocks that require verification in order.",
72                         "For example:",
73                         "    InOrder inOrder = inOrder(mockOne, mockTwo);");
74     }
75 
76     @Test
shouldNotAllowVerifyingInOrderUnfamiliarMocks()77     public void shouldNotAllowVerifyingInOrderUnfamiliarMocks() {
78         InOrder inOrder = inOrder(mock);
79         assertThatThrownBy(
80                         () -> {
81                             inOrder.verify(mockTwo).simpleMethod();
82                         })
83                 .isInstanceOf(MockitoException.class)
84                 .hasMessageContainingAll(
85                         "InOrder can only verify mocks that were passed in during creation of InOrder.",
86                         "For example:",
87                         "    InOrder inOrder = inOrder(mockOne);",
88                         "    inOrder.verify(mockOne).doStuff();");
89     }
90 
91     @Test
shouldReportMissingMethodInvocationWhenStubbing()92     public void shouldReportMissingMethodInvocationWhenStubbing() {
93         when(mock.simpleMethod())
94                 .thenReturn("this stubbing is required to make sure Stubbable is pulled");
95         assertThatThrownBy(
96                         () -> {
97                             when("".toString()).thenReturn("x");
98                         })
99                 .isInstanceOf(MissingMethodInvocationException.class)
100                 .hasMessageContainingAll(
101                         "when() requires an argument which has to be 'a method call on a mock'.",
102                         "For example:",
103                         "    when(mock.getArticles()).thenReturn(articles);",
104                         "Also, this error might show up because:",
105                         "1. you stub either of: final/private/equals()/hashCode() methods.",
106                         "   Those methods *cannot* be stubbed/verified.",
107                         "   Mocking methods declared on non-public parent classes is not supported.",
108                         "2. inside when() you don't call method on mock but on some other object.");
109     }
110 
111     @Test
shouldNotAllowSettingInvalidCheckedException()112     public void shouldNotAllowSettingInvalidCheckedException() {
113         assertThatThrownBy(
114                         () -> {
115                             when(mock.simpleMethod()).thenThrow(new Exception());
116                         })
117                 .isInstanceOf(MockitoException.class)
118                 .hasMessageContainingAll(
119                         "Checked exception is invalid for this method!",
120                         "Invalid: java.lang.Exception");
121     }
122 
123     @Test
shouldNotAllowSettingNullThrowable()124     public void shouldNotAllowSettingNullThrowable() {
125         assertThatThrownBy(
126                         () -> {
127                             when(mock.simpleMethod()).thenThrow(new Throwable[] {null});
128                         })
129                 .isInstanceOf(MockitoException.class)
130                 .hasMessageContaining("Cannot stub with null throwable!");
131     }
132 
133     @SuppressWarnings("all")
134     @Test
shouldNotAllowSettingNullThrowableVararg()135     public void shouldNotAllowSettingNullThrowableVararg() throws Exception {
136         assertThatThrownBy(
137                         () -> {
138                             when(mock.simpleMethod()).thenThrow((Throwable) null);
139                         })
140                 .isInstanceOf(MockitoException.class)
141                 .hasMessageContaining("Cannot stub with null throwable!");
142     }
143 
144     @Test
shouldNotAllowSettingNullConsecutiveThrowable()145     public void shouldNotAllowSettingNullConsecutiveThrowable() {
146         assertThatThrownBy(
147                         () -> {
148                             when(mock.simpleMethod()).thenThrow(new RuntimeException(), null);
149                         })
150                 .isInstanceOf(MockitoException.class)
151                 .hasMessageContaining("Cannot stub with null throwable!");
152     }
153 
154     final class FinalClass {}
155 
156     @Test
shouldNotAllowMockingFinalClassesIfDisabled()157     public void shouldNotAllowMockingFinalClassesIfDisabled() {
158         assertThatThrownBy(
159                         () -> {
160                             mock(FinalClass.class);
161                         })
162                 .isInstanceOf(MockitoException.class)
163                 .hasMessageContainingAll(
164                         "Cannot mock/spy class org.mockitousage.misuse.InvalidUsageTest$FinalClass",
165                         "Mockito cannot mock/spy because :",
166                         " - final class");
167     }
168 
169     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
170     @Test
shouldNotAllowMockingPrimitives()171     public void shouldNotAllowMockingPrimitives() {
172         assertThatThrownBy(
173                         () -> {
174                             mock(Integer.TYPE);
175                         })
176                 .isInstanceOf(MockitoException.class)
177                 .hasMessageContainingAll(
178                         "Cannot mock/spy int",
179                         "Mockito cannot mock/spy because :",
180                         " - primitive type");
181     }
182 
183     interface ObjectLikeInterface {
equals(Object o)184         boolean equals(Object o);
185 
toString()186         String toString();
187 
hashCode()188         int hashCode();
189     }
190 
191     @Test
shouldNotMockObjectMethodsOnInterfaceVerifyNoInteractions()192     public void shouldNotMockObjectMethodsOnInterfaceVerifyNoInteractions() {
193         ObjectLikeInterface inter = mock(ObjectLikeInterface.class);
194 
195         Object ignored = inter.equals(null);
196         ignored = inter.toString();
197         ignored = inter.hashCode();
198 
199         verifyNoInteractions(inter);
200     }
201 
202     @Test
shouldNotMockObjectMethodsOnClassVerifyNoInteractions()203     public void shouldNotMockObjectMethodsOnClassVerifyNoInteractions() {
204         Object clazz = mock(ObjectLikeInterface.class);
205 
206         Object ignored = clazz.equals(null);
207         ignored = clazz.toString();
208         ignored = clazz.hashCode();
209 
210         verifyNoInteractions(clazz);
211     }
212 }
213