/
ListenerManagerAlternativeTest.java
61 lines (48 loc) · 1.56 KB
/
ListenerManagerAlternativeTest.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
package uk.co.webamoeba.mockito.collections.sample;
import java.util.Collection;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.runners.MockitoJUnitRunner;
import uk.co.webamoeba.mockito.collections.MockitoCollections;
import uk.co.webamoeba.mockito.collections.annotation.CollectionOfMocks;
/**
* This test shows how you can use Mockito Collections to test where there is a {@link Collection} of delegates that do
* not return values. Comments are included throughout the file to hellp clarify what's going on.
*
* @author James Kennard
*/
@RunWith(MockitoJUnitRunner.class)
public class ListenerManagerAlternativeTest {
// ListenerManager containing a Collection of SampleListeners
@InjectMocks
private ListenerManager manager;
// A Collection of two Mocks
@CollectionOfMocks(numberOfMocks = 2)
private Collection<Listener> listeners;
// Setup making use of Mockito Collections for injection of handlers
@Before
public void before() {
MockitoCollections.initialise(this);
}
@Test
public void shouldCallAllListeners() {
// Given
String someEvent = "Something";
// When
manager.eventOccurred(someEvent);
// Then
// collectively verify all the listeners were called
MockitoCollections.collectiveVerify(Listener.class, listeners).eventOccured(someEvent);
}
@Test(expected = IllegalArgumentException.class)
public void shouldFailGivenNull() {
// Given
String someEvent = null;
// When
manager.eventOccurred(someEvent);
// Then
// Exception Thrown
}
}