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.mockito.internal.progress; 7 8 import org.assertj.core.api.Assertions; 9 import org.assertj.core.api.ThrowableAssert; 10 import org.junit.Before; 11 import org.junit.Test; 12 import org.mockito.exceptions.base.MockitoException; 13 import org.mockito.exceptions.misusing.RedundantListenerException; 14 import org.mockito.internal.listeners.AutoCleanableListener; 15 import org.mockito.internal.verification.VerificationModeFactory; 16 import org.mockito.listeners.MockitoListener; 17 import org.mockito.verification.VerificationMode; 18 import org.mockitoutil.TestBase; 19 20 import java.util.LinkedHashSet; 21 import java.util.Set; 22 23 import static org.junit.Assert.assertNull; 24 import static org.junit.Assert.assertSame; 25 import static org.junit.Assert.fail; 26 import static org.mockito.Mockito.mock; 27 import static org.mockito.Mockito.when; 28 29 public class MockingProgressImplTest extends TestBase { 30 31 private MockingProgress mockingProgress; 32 33 @Before setup()34 public void setup() { 35 mockingProgress = new MockingProgressImpl(); 36 } 37 38 @Test shouldStartVerificationAndPullVerificationMode()39 public void shouldStartVerificationAndPullVerificationMode() throws Exception { 40 assertNull(mockingProgress.pullVerificationMode()); 41 42 VerificationMode mode = VerificationModeFactory.times(19); 43 44 mockingProgress.verificationStarted(mode); 45 46 assertSame(mode, mockingProgress.pullVerificationMode()); 47 48 assertNull(mockingProgress.pullVerificationMode()); 49 } 50 51 @Test shouldCheckIfVerificationWasFinished()52 public void shouldCheckIfVerificationWasFinished() throws Exception { 53 mockingProgress.verificationStarted(VerificationModeFactory.atLeastOnce()); 54 try { 55 mockingProgress.verificationStarted(VerificationModeFactory.atLeastOnce()); 56 fail(); 57 } catch (MockitoException e) {} 58 } 59 60 @Test shouldNotifyListenerSafely()61 public void shouldNotifyListenerSafely() throws Exception { 62 //when 63 mockingProgress.addListener(null); 64 65 //then no exception is thrown: 66 mockingProgress.mockingStarted(null, null); 67 } 68 69 @Test should_not_allow_redundant_listeners()70 public void should_not_allow_redundant_listeners() { 71 MockitoListener listener1 = mock(MockitoListener.class); 72 final MockitoListener listener2 = mock(MockitoListener.class); 73 74 final Set<MockitoListener> listeners = new LinkedHashSet<MockitoListener>(); 75 76 //when 77 MockingProgressImpl.addListener(listener1, listeners); 78 79 //then 80 Assertions.assertThatThrownBy(new ThrowableAssert.ThrowingCallable() { 81 public void call() { 82 MockingProgressImpl.addListener(listener2, listeners); 83 } 84 }).isInstanceOf(RedundantListenerException.class); 85 } 86 87 @Test should_clean_up_listeners_automatically()88 public void should_clean_up_listeners_automatically() { 89 MockitoListener someListener = mock(MockitoListener.class); 90 MyListener cleanListener = mock(MyListener.class); 91 MyListener dirtyListener = when(mock(MyListener.class).isListenerDirty()).thenReturn(true).getMock(); 92 93 Set<MockitoListener> listeners = new LinkedHashSet<MockitoListener>(); 94 95 //when 96 MockingProgressImpl.addListener(someListener, listeners); 97 MockingProgressImpl.addListener(dirtyListener, listeners); 98 99 //then 100 Assertions.assertThat(listeners).containsExactlyInAnyOrder(someListener, dirtyListener); 101 102 //when 103 MockingProgressImpl.addListener(cleanListener, listeners); 104 105 //then dirty listener was removed automatically 106 Assertions.assertThat(listeners).containsExactlyInAnyOrder(someListener, cleanListener); 107 } 108 109 interface MyListener extends MockitoListener, AutoCleanableListener {} 110 } 111