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