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 java.util.Observer; 8 import org.junit.After; 9 import org.junit.Test; 10 import org.mockito.Mock; 11 import org.mockito.exceptions.misusing.InvalidUseOfMatchersException; 12 import org.mockito.exceptions.misusing.UnfinishedVerificationException; 13 import org.mockitousage.IMethods; 14 import org.mockitoutil.TestBase; 15 16 import static junit.framework.TestCase.fail; 17 import static org.assertj.core.api.Assertions.assertThat; 18 import static org.junit.Assume.assumeTrue; 19 import static org.mockito.Mockito.anyBoolean; 20 import static org.mockito.Mockito.anyInt; 21 import static org.mockito.Mockito.anyObject; 22 import static org.mockito.Mockito.mock; 23 import static org.mockito.Mockito.validateMockitoUsage; 24 import static org.mockito.Mockito.verify; 25 26 public class DetectingMisusedMatchersTest extends TestBase { 27 28 class WithFinal { finalMethod(Object object)29 final Object finalMethod(Object object) { 30 return null; 31 } 32 } 33 34 @Mock private WithFinal withFinal; 35 36 @After resetState()37 public void resetState() { 38 super.resetState(); 39 } 40 misplaced_anyObject_argument_matcher()41 private void misplaced_anyObject_argument_matcher() { 42 anyObject(); 43 } 44 misplaced_anyInt_argument_matcher()45 private void misplaced_anyInt_argument_matcher() { 46 anyInt(); 47 } 48 misplaced_anyBoolean_argument_matcher()49 private void misplaced_anyBoolean_argument_matcher() { 50 anyBoolean(); 51 } 52 53 @Test should_fail_fast_when_argument_matchers_are_abused()54 public void should_fail_fast_when_argument_matchers_are_abused() { 55 misplaced_anyObject_argument_matcher(); 56 try { 57 mock(IMethods.class); 58 fail(); 59 } catch (InvalidUseOfMatchersException e) { 60 assertThat(e).hasMessageContaining("Misplaced or misused argument matcher"); 61 } 62 } 63 64 @Test should_report_argument_locations_when_argument_matchers_misused()65 public void should_report_argument_locations_when_argument_matchers_misused() { 66 try { 67 Observer observer = mock(Observer.class); 68 69 misplaced_anyInt_argument_matcher(); 70 misplaced_anyObject_argument_matcher(); 71 misplaced_anyBoolean_argument_matcher(); 72 73 observer.update(null, null); 74 75 validateMockitoUsage(); 76 fail(); 77 } catch (InvalidUseOfMatchersException e) { 78 assertThat(e) 79 .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyInt_argument_matcher") 80 .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyObject_argument_matcher") 81 .hasMessageContaining("DetectingMisusedMatchersTest.misplaced_anyBoolean_argument_matcher"); 82 } 83 } 84 85 86 @Test shouldSayUnfinishedVerificationButNotInvalidUseOfMatchers()87 public void shouldSayUnfinishedVerificationButNotInvalidUseOfMatchers() { 88 assumeTrue("Does not apply for inline mocks", withFinal.getClass() != WithFinal.class); 89 verify(withFinal).finalMethod(anyObject()); 90 try { 91 verify(withFinal); 92 fail(); 93 } catch (UnfinishedVerificationException e) {} 94 } 95 } 96