forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CapturingMatcherTest.java
74 lines (59 loc) · 2.03 KB
/
CapturingMatcherTest.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import java.util.Iterator;
import org.junit.Test;
import org.mockito.exceptions.base.MockitoException;
import org.mockitoutil.TestBase;
public class CapturingMatcherTest extends TestBase {
@Test
public void should_capture_arguments() throws Exception {
// given
CapturingMatcher<String> m = new CapturingMatcher<String>(String.class);
// when
m.captureFrom("foo");
m.captureFrom("bar");
// then
assertThat(m.getAllValues()).containsSequence("foo", "bar");
}
@Test
public void should_know_last_captured_value() throws Exception {
// given
CapturingMatcher<String> m = new CapturingMatcher<String>(String.class);
// when
m.captureFrom("foo");
m.captureFrom("bar");
// then
assertEquals("bar", m.getLastValue());
}
@Test
public void should_scream_when_nothing_yet_captured() throws Exception {
// given
CapturingMatcher<String> m = new CapturingMatcher<String>(String.class);
try {
// when
m.getLastValue();
// then
fail();
} catch (MockitoException e) {
}
}
@Test
public void should_not_fail_when_used_in_concurrent_tests() throws Exception {
// given
final CapturingMatcher<String> m = new CapturingMatcher<String>(String.class);
// when
m.captureFrom("concurrent access");
Iterator<String> iterator = m.getAllValues().iterator();
m.captureFrom("concurrent access");
// then
assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.next())
.isEqualTo("concurrent access"); // Potential ConcurrentModificationException
}
}