-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
InjectMocksTest.java
98 lines (79 loc) · 3.16 KB
/
InjectMocksTest.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
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.assertj.core.api.Assertions.assertThat;
class ToBeMockedInTestSuperClass{
int identifier;
public ToBeMockedInTestSuperClass(int identifier) {
this.identifier = identifier;
}
}
class ToBeMocked{
int identifier;
public ToBeMocked(int identifier) {
this.identifier = identifier;
}
}
class TestClassToBeInitiatedViaConstructorInSuperClass {
ToBeMockedInTestSuperClass toBeMockedInTestSuperClass;
public TestClassToBeInitiatedViaConstructorInSuperClass(ToBeMockedInTestSuperClass toBeMockedInTestSuperClass) {
assert toBeMockedInTestSuperClass != null;
this.toBeMockedInTestSuperClass = new ToBeMockedInTestSuperClass(42);
}
}
class TestClassToBeInitiatedViaConstructor{
ToBeMockedInTestSuperClass toBeMockedInTestSuperClass;
ToBeMocked toBeMocked;
public TestClassToBeInitiatedViaConstructor(ToBeMocked toBeMocked, ToBeMockedInTestSuperClass toBeMockedInTestSuperClass) {
assert toBeMocked != null;
assert toBeMockedInTestSuperClass != null;
this.toBeMocked = new ToBeMocked(42);
this.toBeMockedInTestSuperClass = new ToBeMockedInTestSuperClass(42);
}
}
class SuperTestClass {
@Mock
ToBeMockedInTestSuperClass toBeMockedInTestSuperClass;
@InjectMocks
TestClassToBeInitiatedViaConstructorInSuperClass testClassToBeInitiatedViaConstructorInSuperClass;
}
@ExtendWith(MockitoExtension.class)
class InjectMocksTest extends SuperTestClass {
@Mock
ToBeMocked toBeMocked;
@InjectMocks
TestClassToBeInitiatedViaConstructor testClassToBeInitiatedViaConstructor;
/**
* Checks that {@link #testClassToBeInitiatedViaConstructor} holds instances that have identifier 42.
* It being 42 is proof that constructor injection was used over field injection.
*/
@Test
void given_instanceToBeInitializedByMockito_when_mocksRequestedByConstructorAreInTestAndSuperClass_should_useConstructorInjection() {
assertThat(testClassToBeInitiatedViaConstructor)
.extracting(
testInstance-> testInstance.toBeMocked.identifier,
testInstance-> testInstance.toBeMockedInTestSuperClass.identifier
)
.containsExactly(
42,
42
);
}
/**
* Checks that {@link #testClassToBeInitiatedViaConstructorInSuperClass} holds instances that have identifier 42.
* It being 42 is proof that constructor injection was used over field injection.
*/
@Test
public void given_instanceInSuperClassToBeInitializedByMockito_when_mocksRequestedAreInSuperClass_should_useConstructorInjection(){
assertThat(testClassToBeInitiatedViaConstructorInSuperClass)
.extracting(yetAnotherClas1 -> yetAnotherClas1.toBeMockedInTestSuperClass.identifier)
.isEqualTo(42);
}
}