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.verification; 7 8 import org.junit.Test; 9 import org.mockito.InOrder; 10 import org.mockito.Mock; 11 import org.mockito.exceptions.misusing.NotAMockException; 12 import org.mockito.exceptions.verification.NoInteractionsWanted; 13 import org.mockitousage.IMethods; 14 import org.mockitoutil.TestBase; 15 16 import static junit.framework.TestCase.fail; 17 import static org.mockito.Mockito.*; 18 19 @SuppressWarnings("unchecked") 20 public class VerificationExcludingStubsTest extends TestBase { 21 22 @Mock IMethods mock; 23 24 @Test shouldAllowToExcludeStubsForVerification()25 public void shouldAllowToExcludeStubsForVerification() throws Exception { 26 //given 27 when(mock.simpleMethod()).thenReturn("foo"); 28 29 //when 30 String stubbed = mock.simpleMethod(); //irrelevant call because it is stubbing 31 mock.objectArgMethod(stubbed); 32 33 //then 34 verify(mock).objectArgMethod("foo"); 35 36 //verifyNoMoreInteractions fails: 37 try { verifyNoMoreInteractions(mock); fail(); } catch (NoInteractionsWanted e) {} 38 39 //but it works when stubs are ignored: 40 ignoreStubs(mock); 41 verifyNoMoreInteractions(mock); 42 } 43 44 @Test shouldExcludeFromVerificationInOrder()45 public void shouldExcludeFromVerificationInOrder() throws Exception { 46 //given 47 when(mock.simpleMethod()).thenReturn("foo"); 48 49 //when 50 mock.objectArgMethod("1"); 51 mock.objectArgMethod("2"); 52 mock.simpleMethod(); //calling the stub 53 54 //then 55 InOrder inOrder = inOrder(ignoreStubs(mock)); 56 inOrder.verify(mock).objectArgMethod("1"); 57 inOrder.verify(mock).objectArgMethod("2"); 58 inOrder.verifyNoMoreInteractions(); 59 verifyNoMoreInteractions(mock); 60 } 61 62 @Test(expected = NotAMockException.class) shouldIgnoringStubsDetectNulls()63 public void shouldIgnoringStubsDetectNulls() throws Exception { 64 ignoreStubs(mock, null); 65 } 66 67 @Test(expected = NotAMockException.class) shouldIgnoringStubsDetectNonMocks()68 public void shouldIgnoringStubsDetectNonMocks() throws Exception { 69 ignoreStubs(mock, new Object()); 70 } 71 72 } 73