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