• 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 
6 package org.mockitousage.matchers;
7 
8 import org.junit.Test;
9 import org.junit.runner.JUnitCore;
10 import org.junit.runner.Result;
11 import org.junit.runner.RunWith;
12 import org.mockito.AdditionalMatchers;
13 import org.mockito.Mockito;
14 import org.mockito.StateMaster;
15 import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
16 import org.mockito.junit.MockitoJUnitRunner;
17 import org.mockitousage.IMethods;
18 
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.junit.Assert.fail;
21 import static org.mockito.ArgumentMatchers.any;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.doNothing;
24 import static org.mockito.Mockito.when;
25 
26 @RunWith(MockitoJUnitRunner.Silent.class)
27 public class InvalidUseOfMatchersTest {
28 
29     private IMethods mock = Mockito.mock(IMethods.class);
30 
31     @Test
should_detect_wrong_number_of_matchers_when_stubbing()32     public void should_detect_wrong_number_of_matchers_when_stubbing() {
33         when(mock.threeArgumentMethod(1, "2", "3")).thenReturn(null);
34         try {
35             when(mock.threeArgumentMethod(1, eq("2"), "3")).thenReturn(null);
36             fail();
37         } catch (InvalidUseOfMatchersException e) {
38             assertThat(e.getMessage())
39                     .contains("3 matchers expected")
40                     .contains("1 recorded");
41         }
42     }
43 
44     @Test
should_detect_stupid_use_of_matchers_when_verifying()45     public void should_detect_stupid_use_of_matchers_when_verifying() {
46         mock.oneArg(true);
47         eq("that's the stupid way");
48         eq("of using matchers");
49         try {
50             Mockito.verify(mock).oneArg(true);
51             fail();
52         } catch (InvalidUseOfMatchersException e) {
53             assertThat(e.getMessage())
54                     .contains("Misplaced or misused argument matcher detected here");
55             e.printStackTrace();
56         }
57     }
58 
59     @Test
should_not_scream_on_correct_usage()60     public void should_not_scream_on_correct_usage() throws Exception {
61         mock.simpleMethod(AdditionalMatchers.not(eq("asd")));
62         mock.simpleMethod(AdditionalMatchers.or(eq("jkl"), eq("asd")));
63     }
64 
65     @Test
should_scream_when_no_matchers_inside_not()66     public void should_scream_when_no_matchers_inside_not() {
67         try {
68             mock.simpleMethod(AdditionalMatchers.not("jkl"));
69             fail();
70         } catch (InvalidUseOfMatchersException e) {
71             assertThat(e.getMessage())
72                     .contains("No matchers found for")
73                     .containsIgnoringCase("Not(?)");
74         }
75     }
76 
77     @Test
should_scream_when_not_enough_matchers_inside_or_AddtionalMatcher()78     public void should_scream_when_not_enough_matchers_inside_or_AddtionalMatcher() {
79         try {
80             mock.simpleMethod(AdditionalMatchers.or(eq("jkl"), "asd"));
81             fail();
82         } catch (InvalidUseOfMatchersException e) {
83             assertThat(e.getMessage())
84                     .containsIgnoringCase("inside additional matcher Or(?)")
85                     .contains("2 sub matchers expected")
86                     .contains("1 recorded");
87         }
88     }
89 
90     @Test
should_scream_when_Matchers_count_dont_match_parameter_count()91     public void should_scream_when_Matchers_count_dont_match_parameter_count() {
92         try {
93             mock.threeArgumentMethod(1, "asd", eq("asd"));
94             fail();
95         } catch (InvalidUseOfMatchersException e) {
96             assertThat(e.getMessage())
97                     .contains("3 matchers expected")
98                     .contains("1 recorded");
99         }
100     }
101 
102     @Test
should_mention_matcher_when_misuse_detected()103     public void should_mention_matcher_when_misuse_detected() {
104         // Given
105 
106 
107         // When
108         Result run = new JUnitCore().run(ObjectMatcherMisuseOnPrimitiveSite.class);
109 
110         // Then
111 
112         assertThat(run.getFailures()).hasSize(2);
113         assertThat(run.getFailures().get(0).getException()).isInstanceOf(NullPointerException.class)
114                                                            .hasMessage(null);
115         assertThat(run.getFailures().get(1).getException()).isInstanceOf(InvalidUseOfMatchersException.class)
116                                                            .hasMessageContaining("primitive alternatives");
117         new StateMaster().reset();
118 
119     }
120 
121     @RunWith(MockitoJUnitRunner.class)
122     public static class ObjectMatcherMisuseOnPrimitiveSite {
123         @Test
fails_with_NPE()124         public void fails_with_NPE() {
125             IMethods mock = Mockito.mock(IMethods.class);
126             doNothing().when(mock)
127                        .twoArgumentMethod(eq(73),
128                                           (Integer) any()); // <= Raise NPE on this call site
129         }
130 
131     }
132 }
133