/
TestedAndInjectablesTest.java
122 lines (94 loc) · 2.96 KB
/
TestedAndInjectablesTest.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
/*
* Copyright (c) 2006-2015 Rogério Liesenfeld
* This file is subject to the terms of the MIT license (see LICENSE.txt).
*/
package mockit.integration.testng;
import org.testng.annotations.*;
import static org.testng.Assert.*;
import mockit.*;
public final class TestedAndInjectablesTest
{
static final class UtilityClass
{
String name;
Collaborator collaborator1;
Collaborator collaborator2;
}
static class Collaborator { void doSomething() {} }
static class SUT
{
final Collaborator collaborator1;
Collaborator collaborator2;
SUT(Collaborator collaborator1) { this.collaborator1 = collaborator1; }
void useCollaborators()
{
collaborator1.doSomething();
collaborator2.doSomething();
}
}
@Tested(availableDuringSetup = true) UtilityClass util;
@Injectable("util") String utilName;
@Tested SUT tested1;
@Injectable Collaborator collaborator1;
@Tested SUT tested2;
@Tested final SUT tested3 = new SUT(new Collaborator());
@Tested final SUT tested4 = null;
SUT firstTestedObject;
Collaborator firstMockedObject;
Collaborator secondMockedObject;
@BeforeMethod
public void setUp()
{
assertUtilObjectIsAvailable();
tested2 = new SUT(new Collaborator());
}
void assertUtilObjectIsAvailable()
{
assertNotNull(util);
assertEquals(util.name, "util");
assertSame(collaborator1, util.collaborator1);
}
@AfterMethod
public void tearDown()
{
assertUtilObjectIsAvailable();
}
@Test
public void firstTest(@Injectable final Collaborator collaborator2)
{
assertSame(collaborator1, util.collaborator1);
assertNull(util.collaborator2);
assertNotNull(tested1);
firstTestedObject = tested1;
assertNotNull(collaborator1);
firstMockedObject = collaborator1;
assertNotNull(collaborator2);
secondMockedObject = collaborator2;
assertStatesOfTestedObjects(collaborator2);
new Expectations() {{
collaborator1.doSomething();
collaborator2.doSomething();
}};
tested1.useCollaborators();
}
void assertStatesOfTestedObjects(Collaborator collaborator2)
{
assertSame(tested1.collaborator1, collaborator1);
assertSame(tested1.collaborator2, collaborator2);
assertNotSame(tested2.collaborator1, collaborator1);
assertSame(tested2.collaborator2, collaborator2);
assertNotSame(tested3.collaborator1, collaborator1);
assertNotNull(tested3.collaborator2);
assertNull(tested4);
}
@Test(dependsOnMethods = "firstTest")
public void secondTest(@Injectable Collaborator collaborator2)
{
assertSame(collaborator1, util.collaborator1);
assertNull(util.collaborator2);
assertNotSame(collaborator2, secondMockedObject);
assertSame(collaborator1, firstMockedObject);
assertNotSame(tested1, firstTestedObject);
assertStatesOfTestedObjects(collaborator2);
}
}