forked from mockito/mockito
/
MockingProgressImplTest.java
99 lines (71 loc) · 2.97 KB
/
MockingProgressImplTest.java
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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.progress;
import static junit.framework.TestCase.assertNull;
import static junit.framework.TestCase.assertSame;
import static junit.framework.TestCase.fail;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.verification.VerificationEventImpl;
import org.mockito.internal.verification.VerificationModeFactory;
import org.mockito.listeners.VerificationListener;
import org.mockito.verification.VerificationEvent;
import org.mockito.verification.VerificationMode;
public class MockingProgressImplTest {
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 fireVerificationEvent_noVerificationListenerRegistered_shouldThrowNoException() {
VerificationEvent event = new VerificationEventImpl(null, null, null, null);
mockingProgress.fireVerificationEvent(event);
}
@Test
public void fireVerificationEvent_verificationListenerRegistered_shouldBeNotified() {
AssertableVerificationListener listener = new AssertableVerificationListener();
mockingProgress.addListener(listener);
VerificationEvent event = new VerificationEventImpl(null, null, null, null);
mockingProgress.fireVerificationEvent(event);
listener.assertLastEventIsSameAs(event);
}
private static class AssertableVerificationListener implements VerificationListener {
private VerificationEvent lastEvent;
@Override
public void onVerification(VerificationEvent verificationEvent) {
lastEvent = verificationEvent;
}
public void assertLastEventIsSameAs(VerificationEvent expected) {
assertThat(lastEvent).isSameAs(expected);
}
}
}