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.stacktrace; 7 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.UnfinishedStubbingException; 13 import org.mockito.exceptions.misusing.UnfinishedVerificationException; 14 import org.mockitousage.IMethods; 15 import org.mockitoutil.TestBase; 16 17 import static org.junit.Assert.fail; 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static org.mockito.Mockito.*; 20 21 public class ClickableStackTracesWhenFrameworkMisusedTest extends TestBase { 22 23 @Mock private IMethods mock; 24 25 @After resetState()26 public void resetState() { 27 super.resetState(); 28 } 29 misplacedArgumentMatcherHere()30 private void misplacedArgumentMatcherHere() { 31 anyString(); 32 } 33 34 @Test shouldPointOutMisplacedMatcher()35 public void shouldPointOutMisplacedMatcher() { 36 misplacedArgumentMatcherHere(); 37 try { 38 verify(mock).simpleMethod(); 39 fail(); 40 } catch (InvalidUseOfMatchersException e) { 41 assertThat(e) 42 .hasMessageContaining("-> at ") 43 .hasMessageContaining("misplacedArgumentMatcherHere("); 44 } 45 } 46 47 @SuppressWarnings({"MockitoUsage", "CheckReturnValue"}) unfinishedStubbingHere()48 private void unfinishedStubbingHere() { 49 when(mock.simpleMethod()); 50 } 51 52 @Test shouldPointOutUnfinishedStubbing()53 public void shouldPointOutUnfinishedStubbing() { 54 unfinishedStubbingHere(); 55 56 try { 57 verify(mock).simpleMethod(); 58 fail(); 59 } catch (UnfinishedStubbingException e) { 60 assertThat(e) 61 .hasMessageContaining("-> at ") 62 .hasMessageContaining("unfinishedStubbingHere("); 63 } 64 } 65 66 @Test shouldShowWhereIsUnfinishedVerification()67 public void shouldShowWhereIsUnfinishedVerification() throws Exception { 68 unfinishedVerificationHere(); 69 try { 70 mock(IMethods.class); 71 fail(); 72 } catch (UnfinishedVerificationException e) { 73 assertThat(e).hasMessageContaining("unfinishedVerificationHere("); 74 } 75 } 76 77 @SuppressWarnings({"MockitoUsage", "CheckReturnValue"}) unfinishedVerificationHere()78 private void unfinishedVerificationHere() { 79 verify(mock); 80 } 81 } 82