forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockInjectionTest.java
135 lines (111 loc) · 4.24 KB
/
MockInjectionTest.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.configuration;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
import java.lang.reflect.Field;
import java.util.Collections;
import java.util.Observer;
import java.util.Set;
import org.junit.After;
import org.junit.Test;
import org.mockito.internal.configuration.injection.MockInjection;
@SuppressWarnings("unchecked")
public class MockInjectionTest {
private AnObjectWithConstructor withConstructor;
private AnObjectWithoutConstructor withoutConstructor;
@After
public void reset() throws Exception {
withConstructor = null;
withoutConstructor = null;
}
@Test
public void should_not_allow_null_on_field() {
assertThatThrownBy(
() -> {
MockInjection.onField((Field) null, this);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("item in fields should not be null");
}
@Test
public void should_not_allow_null_on_fields() {
assertThatThrownBy(
() -> {
MockInjection.onFields((Set<Field>) null, this);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("fields should not be null");
}
@Test
public void should_not_allow_null_on_instance_owning_the_field() {
assertThatThrownBy(
() -> {
MockInjection.onField(field("withConstructor"), null);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("fieldOwner should not be null");
}
@Test
public void should_not_allow_null_on_mocks() {
assertThatThrownBy(
() -> {
MockInjection.onField(field("withConstructor"), this).withMocks(null);
})
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("mocks should not be null");
}
@Test
public void can_try_constructor_injection() throws Exception {
MockInjection.onField(field("withConstructor"), this)
.withMocks(oneSetMock())
.tryConstructorInjection()
.apply();
assertThat(withConstructor.initializedWithConstructor).isTrue();
}
@Test
public void should_not_fail_if_constructor_injection_is_not_possible() throws Exception {
MockInjection.onField(field("withoutConstructor"), this)
.withMocks(otherKindOfMocks())
.tryConstructorInjection()
.apply();
assertThat(withoutConstructor).isNull();
}
@Test
public void can_try_property_or_setter_injection() throws Exception {
MockInjection.onField(field("withoutConstructor"), this)
.withMocks(oneSetMock())
.tryPropertyOrFieldInjection()
.apply();
assertThat(withoutConstructor.theSet).isNotNull();
}
@Test
public void should_not_fail_if_property_or_field_injection_is_not_possible() throws Exception {
MockInjection.onField(field("withoutConstructor"), this)
.withMocks(otherKindOfMocks())
.tryPropertyOrFieldInjection()
.apply();
assertThat(withoutConstructor.theSet).isNull();
}
private Set oneSetMock() {
return Collections.singleton(mock(Set.class));
}
private Set otherKindOfMocks() {
return Collections.singleton(mock(Observer.class));
}
private Field field(String field) throws NoSuchFieldException {
return getClass().getDeclaredField(field);
}
public static class AnObjectWithConstructor {
public boolean initializedWithConstructor = false;
public AnObjectWithConstructor(Set<String> strings) {
initializedWithConstructor = true;
}
}
public static class AnObjectWithoutConstructor {
private Set<?> theSet;
}
}