• 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.verify;
11 import static org.mockito.Mockito.verifyNoMoreInteractions;
12 
13 import org.junit.Test;
14 import org.mockito.Mock;
15 import org.mockito.exceptions.base.MockitoException;
16 import org.mockito.exceptions.misusing.NotAMockException;
17 import org.mockito.exceptions.misusing.NullInsteadOfMockException;
18 import org.mockitousage.IMethods;
19 import org.mockitoutil.TestBase;
20 
21 public class DescriptiveMessagesOnMisuseTest extends TestBase {
22 
23     @Mock private IMethods mock;
24 
25     class Foo {
finalMethod()26         public final String finalMethod() {
27             return null;
28         }
29     }
30 
31     @SuppressWarnings("all")
32     @Test
tryDescriptiveMessagesOnMisuse()33     public void tryDescriptiveMessagesOnMisuse() {
34         Foo foo = mock(Foo.class);
35 
36         //        when(foo.finalMethod()).thenReturn("foo");
37         //        doReturn("foo").when(foo).finalMethod();
38         //        verify(foo).finalMethod();
39 
40         //        doReturn("foo");
41         //        doReturn("bar");
42 
43         //        verifyNoMoreInteractions();
44         //        verifyNoMoreInteractions(null);
45         //        verifyNoMoreInteractions("");
46         //
47         //        inOrder();
48         //        inOrder(null);
49         //        inOrder("test");
50         //        InOrder inOrder = inOrder(mock(List.class));
51         //        inOrder.verify(mock).simpleMethod();
52 
53         //        verify(null);
54         //        verify(mock.booleanReturningMethod());
55 
56         //        verify(mock).varargs("test", anyString());
57 
58         //        when("x").thenReturn("x");
59 
60         //        when(mock.simpleMethod());
61         //        when(mock.differentMethod()).thenReturn("");
62     }
63 
64     @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
65     @Test
shouldScreamWhenWholeMethodPassedToVerify()66     public void shouldScreamWhenWholeMethodPassedToVerify() {
67         assertThatThrownBy(
68                         () -> {
69                             verify(mock.booleanReturningMethod());
70                         })
71                 .isInstanceOf(NotAMockException.class)
72                 .hasMessageContainingAll(
73                         "Argument passed to verify() is of type Boolean and is not a mock!",
74                         "Make sure you place the parenthesis correctly!",
75                         "See the examples of correct verifications:",
76                         "    verify(mock).someMethod();",
77                         "    verify(mock, times(10)).someMethod();",
78                         "    verify(mock, atLeastOnce()).someMethod();");
79     }
80 
81     @Test
shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions()82     public void shouldScreamWhenWholeMethodPassedToVerifyNoMoreInteractions() {
83         assertThatThrownBy(
84                         () -> {
85                             verifyNoMoreInteractions(mock.byteReturningMethod());
86                         })
87                 .isInstanceOf(NotAMockException.class)
88                 .hasMessageContainingAll(
89                         "Argument(s) passed is not a mock!",
90                         "Examples of correct verifications:",
91                         "    verifyNoMoreInteractions(mockOne, mockTwo);",
92                         "    verifyNoInteractions(mockOne, mockTwo);");
93     }
94 
95     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
96     @Test
shouldScreamWhenInOrderCreatedWithDodgyMock()97     public void shouldScreamWhenInOrderCreatedWithDodgyMock() {
98         assertThatThrownBy(
99                         () -> {
100                             inOrder("not a mock");
101                         })
102                 .isInstanceOf(NotAMockException.class)
103                 .hasMessageContainingAll(
104                         "Argument(s) passed is not a mock!",
105                         "Pass mocks that require verification in order.",
106                         "For example:",
107                         "    InOrder inOrder = inOrder(mockOne, mockTwo);");
108     }
109 
110     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
111     @Test
shouldScreamWhenInOrderCreatedWithNulls()112     public void shouldScreamWhenInOrderCreatedWithNulls() {
113         assertThatThrownBy(
114                         () -> {
115                             inOrder(mock, null);
116                         })
117                 .isInstanceOf(NullInsteadOfMockException.class)
118                 .hasMessageContainingAll(
119                         "Argument(s) passed is null!",
120                         "Pass mocks that require verification in order.",
121                         "For example:",
122                         "    InOrder inOrder = inOrder(mockOne, mockTwo);");
123     }
124 
125     @SuppressWarnings({"MockitoUsage", "CheckReturnValue"})
126     @Test
shouldScreamNullPassedToVerify()127     public void shouldScreamNullPassedToVerify() {
128         assertThatThrownBy(
129                         () -> {
130                             verify(null);
131                         })
132                 .isInstanceOf(NullInsteadOfMockException.class)
133                 .hasMessageContainingAll(
134                         "Argument passed to verify() should be a mock but is null!",
135                         "Examples of correct verifications:",
136                         "    verify(mock).someMethod();",
137                         "    verify(mock, times(10)).someMethod();",
138                         "    verify(mock, atLeastOnce()).someMethod();",
139                         "    not: verify(mock.someMethod());",
140                         "Also, if you use @Mock annotation don't miss openMocks()");
141     }
142 
143     @Test
shouldScreamWhenNotMockPassedToVerifyNoMoreInteractions()144     public void shouldScreamWhenNotMockPassedToVerifyNoMoreInteractions() {
145         assertThatThrownBy(
146                         () -> {
147                             verifyNoMoreInteractions(null, "blah");
148                         })
149                 .isInstanceOf(NullInsteadOfMockException.class)
150                 .hasMessageContainingAll(
151                         "Argument(s) passed is null!",
152                         "Examples of correct verifications:",
153                         "    verifyNoMoreInteractions(mockOne, mockTwo);",
154                         "    verifyNoInteractions(mockOne, mockTwo);");
155     }
156 
157     @SuppressWarnings("all")
158     @Test
shouldScreamWhenNullPassedToVerifyNoMoreInteractions()159     public void shouldScreamWhenNullPassedToVerifyNoMoreInteractions() {
160         assertThatThrownBy(
161                         () -> {
162                             verifyNoMoreInteractions((Object[]) null);
163                         })
164                 .isInstanceOf(MockitoException.class)
165                 .hasMessageContainingAll(
166                         "Method requires argument(s)!",
167                         "Pass mocks that should be verified, e.g:",
168                         "    verifyNoMoreInteractions(mockOne, mockTwo);",
169                         "    verifyNoInteractions(mockOne, mockTwo);");
170     }
171 }
172