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.assertj.core.api.Assertions; 9 import org.junit.After; 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.mockito.InOrder; 13 import org.mockito.Mock; 14 import org.mockito.exceptions.base.MockitoException; 15 import org.mockito.exceptions.verification.NoInteractionsWanted; 16 import org.mockito.exceptions.verification.VerificationInOrderFailure; 17 import org.mockito.exceptions.verification.WantedButNotInvoked; 18 import org.mockitousage.IMethods; 19 import org.mockitoutil.TestBase; 20 21 import static junit.framework.TestCase.fail; 22 import static org.mockito.Mockito.inOrder; 23 import static org.mockito.Mockito.verify; 24 import static org.mockito.Mockito.verifyNoMoreInteractions; 25 import static org.mockito.Mockito.verifyZeroInteractions; 26 import static org.mockito.Mockito.when; 27 import static org.mockitoutil.Conditions.firstMethodInStackTrace; 28 29 public class StackTraceFilteringTest extends TestBase { 30 31 @Mock private IMethods mock; 32 33 @After resetState()34 public void resetState() { 35 super.resetState(); 36 } 37 38 @Before setup()39 public void setup() { 40 makeStackTracesClean(); 41 } 42 43 @Test shouldFilterStackTraceOnVerify()44 public void shouldFilterStackTraceOnVerify() { 45 try { 46 verify(mock).simpleMethod(); 47 fail(); 48 } catch (WantedButNotInvoked e) { 49 Assertions.assertThat(e).has(firstMethodInStackTrace("shouldFilterStackTraceOnVerify")); 50 } 51 } 52 53 @Test shouldFilterStackTraceOnVerifyNoMoreInteractions()54 public void shouldFilterStackTraceOnVerifyNoMoreInteractions() { 55 mock.oneArg(true); 56 try { 57 verifyNoMoreInteractions(mock); 58 fail(); 59 } catch (NoInteractionsWanted e) { 60 Assertions.assertThat(e).has(firstMethodInStackTrace("shouldFilterStackTraceOnVerifyNoMoreInteractions")); 61 } 62 } 63 64 @Test shouldFilterStackTraceOnVerifyZeroInteractions()65 public void shouldFilterStackTraceOnVerifyZeroInteractions() { 66 mock.oneArg(true); 67 try { 68 verifyZeroInteractions(mock); 69 fail(); 70 } catch (NoInteractionsWanted e) { 71 Assertions.assertThat(e).has(firstMethodInStackTrace("shouldFilterStackTraceOnVerifyZeroInteractions")); 72 } 73 } 74 75 @Test shouldFilterStacktraceOnMockitoException()76 public void shouldFilterStacktraceOnMockitoException() { 77 verify(mock); 78 try { 79 verify(mock).oneArg(true); 80 fail(); 81 } catch (MockitoException expected) { 82 Assertions.assertThat(expected).has(firstMethodInStackTrace("shouldFilterStacktraceOnMockitoException")); 83 } 84 } 85 86 @Test shouldFilterStacktraceWhenVerifyingInOrder()87 public void shouldFilterStacktraceWhenVerifyingInOrder() { 88 InOrder inOrder = inOrder(mock); 89 mock.oneArg(true); 90 mock.oneArg(false); 91 92 inOrder.verify(mock).oneArg(false); 93 try { 94 inOrder.verify(mock).oneArg(true); 95 fail(); 96 } catch (VerificationInOrderFailure e) { 97 Assertions.assertThat(e).has(firstMethodInStackTrace("shouldFilterStacktraceWhenVerifyingInOrder")); 98 } 99 } 100 101 @Test shouldFilterStacktraceWhenInOrderThrowsMockitoException()102 public void shouldFilterStacktraceWhenInOrderThrowsMockitoException() { 103 try { 104 inOrder(); 105 fail(); 106 } catch (MockitoException expected) { 107 Assertions.assertThat(expected).has(firstMethodInStackTrace("shouldFilterStacktraceWhenInOrderThrowsMockitoException")); 108 } 109 } 110 111 @Test shouldFilterStacktraceWhenInOrderVerifies()112 public void shouldFilterStacktraceWhenInOrderVerifies() { 113 try { 114 InOrder inOrder = inOrder(mock); 115 inOrder.verify(null); 116 fail(); 117 } catch (MockitoException expected) { 118 Assertions.assertThat(expected).has(firstMethodInStackTrace("shouldFilterStacktraceWhenInOrderVerifies")); 119 } 120 } 121 122 @Test shouldFilterStackTraceWhenThrowingExceptionFromMockHandler()123 public void shouldFilterStackTraceWhenThrowingExceptionFromMockHandler() { 124 try { 125 when(mock.oneArg(true)).thenThrow(new Exception()); 126 fail(); 127 } catch (MockitoException expected) { 128 Assertions.assertThat(expected).has(firstMethodInStackTrace("shouldFilterStackTraceWhenThrowingExceptionFromMockHandler")); 129 } 130 } 131 132 @Test shouldShowProperExceptionStackTrace()133 public void shouldShowProperExceptionStackTrace() throws Exception { 134 when(mock.simpleMethod()).thenThrow(new RuntimeException()); 135 136 try { 137 mock.simpleMethod(); 138 fail(); 139 } catch (RuntimeException e) { 140 Assertions.assertThat(e).has(firstMethodInStackTrace("shouldShowProperExceptionStackTrace")); 141 } 142 } 143 } 144