1 /* 2 * Copyright (c) 2017 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockitousage.verification; 6 7 import org.assertj.core.api.Assertions; 8 import org.junit.Test; 9 import org.mockito.Mockito; 10 import org.mockito.exceptions.base.MockitoException; 11 import org.mockito.listeners.VerificationStartedEvent; 12 import org.mockito.listeners.VerificationStartedListener; 13 import org.mockitoutil.TestBase; 14 15 import java.util.ArrayList; 16 import java.util.List; 17 18 import static org.junit.Assert.assertEquals; 19 import static org.junit.Assert.fail; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.never; 22 import static org.mockito.Mockito.verify; 23 24 /** 25 * This test demonstrates how verification started listeners work. 26 * The test cases are contrived but they provide necessary coverage. 27 * For rationale, see https://github.com/mockito/mockito/issues/1191 28 */ 29 public class VerificationStartedListenerTest extends TestBase { 30 31 @Test 32 public void verified_mock_can_be_replaced() throws Exception { 33 //given 34 final List mock1 = mock(List.class); 35 mock1.clear(); //register clear() on mock1 36 37 //when 38 List mock2 = mock(List.class, Mockito.withSettings().verificationStartedListeners(new VerificationStartedListener() { 39 public void onVerificationStarted(VerificationStartedEvent event) { 40 //this is a hack to simulate desired behavior 41 event.setMock(mock1); 42 } 43 })); 44 45 //then verified mock is not mock2 that was passed to 'verify' but the replacement: mock1 46 List verifiedMock = verify(mock2); 47 assertEquals(mock1, verifiedMock); 48 49 //and verification is successful because mock1.clear() was called 50 verifiedMock.clear(); 51 52 //this test is admittedly contrived. it's goal is to provide coverage for the key functionality of the listener 53 //see the discussion at https://github.com/mockito/mockito/issues/1191 54 } 55 56 @Test 57 public void verification_started_event_contains_correct_mock() throws Exception { 58 //given 59 final List<Object> container = new ArrayList<Object>(); 60 61 List mock = mock(List.class, Mockito.withSettings().verificationStartedListeners(new VerificationStartedListener() { 62 public void onVerificationStarted(VerificationStartedEvent event) { 63 //this is a hack to simulate desired behavior 64 container.add(event.getMock()); 65 } 66 })); 67 68 //when 69 verify(mock, never()).clear(); 70 71 //then 72 Assertions.assertThat(container).containsExactly(mock); 73 } 74 75 @Test 76 public void listeners_are_executed_in_sequence() throws Exception { 77 //given 78 final List<Object> container = new ArrayList<Object>(); 79 final List mock1 = mock(List.class); 80 81 List mock2 = mock(List.class, Mockito.withSettings().verificationStartedListeners(new VerificationStartedListener() { 82 public void onVerificationStarted(VerificationStartedEvent event) { 83 //this is a hack to simulate desired behavior 84 container.add(event.getMock()); 85 event.setMock(mock1); 86 } 87 }, new VerificationStartedListener() { 88 @Override 89 public void onVerificationStarted(VerificationStartedEvent event) { 90 container.add(event.getMock()); 91 } 92 })); 93 94 //when 95 verify(mock2, never()).clear(); 96 97 //ensure that: 98 // 1. listeners were notified in sequence 99 // 2. the state set by 1st listeners affects 2nd listener 100 Assertions.assertThat(container).containsExactly(mock2, mock1); 101 102 //there is no particular reason we decided on that behavior 103 //we want to have a consistent and documented behavior of the verification started listener 104 } 105 106 @Test 107 public void shows_clean_exception_when_null_array_passed() throws Exception { 108 try { 109 //when 110 Mockito.withSettings().verificationStartedListeners(null); 111 fail(); 112 } catch (MockitoException e) { 113 assertEquals("verificationStartedListeners() does not accept null vararg array. See the Javadoc.", e.getMessage()); 114 } 115 } 116 117 @Test 118 public void shows_clean_exception_when_null_listener_passed() throws Exception { 119 try { 120 //when 121 Mockito.withSettings().verificationStartedListeners(mock(VerificationStartedListener.class), null); 122 fail(); 123 } catch (MockitoException e) { 124 assertEquals("verificationStartedListeners() does not accept null listeners. See the Javadoc.", e.getMessage()); 125 } 126 } 127 } 128