Repository navigation
Expand file tree
/
Copy pathTestModel.java
More file actions
143 lines (102 loc) · 4.21 KB
/
Copy pathTestModel.java
File metadata and controls
143 lines (102 loc) · 4.21 KB
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
136
137
138
139
140
141
142
143
package org.codebrewery;
import static org.easymock.EasyMock.expect;
import static org.junit.Assert.assertEquals;
import static org.powermock.api.easymock.PowerMock.mockStatic;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* Created by ejeserl on 9/20/15.
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest(ApiFactory.class)
public class TestModel {
private ApiConfig apiConfig;
private MockDefaultApiImplementation mockApiImpl;
/**
* All these tests will use powermock.
*
*/
public void setUpWithMock() {
setUpWithMock("plainPluto.json", null);
}
public void setUpWithMock(String fileName) {
setUpWithMock(fileName, null);
}
public void setUpWithMock(Exception exception) {
setUpWithMock(null, exception);
}
public void setUpWithMock(String fileName, Exception e) {
// 1. create config
// 2. create mock
// 3. init mock
apiConfig = new ApiConfig.ConfigBuilder().apiLocation("api").port("8081").host("localhost").build();
mockApiImpl = new MockDefaultApiImplementation(apiConfig, fileName);
// We create a new instance of test class under test as usually.
// This is the way to tell PowerMock to mock all static methods of a
// given class
mockStatic(ApiFactory.class);
expect(ApiFactory.getDefaultImplementation()).andReturn(mockApiImpl);
// Note how we replay the class, not the instance!
PowerMock.replay(ApiFactory.class);
}
@Test
public void save_model() throws JavaOrmenException {
setUpWithMock();
Model model = new DogModel();
DogModel changedModel = (DogModel) model.save();
// Note how we verify the class, not the instance!
PowerMock.verifyAll();
// Assert that the ID is correct
assertEquals("pluto", changedModel.getName());
assertEquals("http://localhost:8081/api/dogs", mockApiImpl.getLatestExecutedRequestBuilder().getUrl());
}
@Test
public void delete_model() throws JavaOrmenException {
setUpWithMock();
Model model = new DogModel("pluto", 123);
model.delete();
// Note how we verify the class, not the instance!
PowerMock.verifyAll();
// Assert that the ID is correct
assertEquals("http://localhost:8081/api/dogs/pluto", mockApiImpl.getLatestExecutedRequestBuilder().getUrl());
}
@Test
public void fetch_model() throws JavaOrmenException {
setUpWithMock();
DogModel model = new DogModel("plutoXII", 4);
DogModel changedModel = (DogModel) model.fetch();
// Note how we verify the class, not the instance!
PowerMock.verifyAll();
// Assert that the ID is correct
assertEquals("pluto", changedModel.getName());
assertEquals("http://localhost:8081/api/dogs/plutoXII", mockApiImpl.getLatestExecutedRequestBuilder().getUrl());
// verify that the returned model has an different name.
assertEquals("pluto", changedModel.getName());
// verify that the old model still got the old name
assertEquals("plutoXII", model.getName());
}
@Test
public void update_model() throws JavaOrmenException {
setUpWithMock();
Model model = new DogModel("plutoXII", 1234);
DogModel changedModel = model.update();
// Note how we verify the class, not the instance!
PowerMock.verifyAll();
assertEquals("http://localhost:8081/api/dogs/plutoXII", mockApiImpl.getLatestExecutedRequestBuilder().getUrl());
assertEquals(3, changedModel.getAge());
}
@Test
public void find_all_instances() throws JavaOrmenException {
setUpWithMock("twoPlutosInAList.json");
List<DogModel> listOfDogs = DogModel.find.all();
// Note how we verify the class, not the instance!
PowerMock.verifyAll();
// Assert that the ID is correct
assertEquals(1, listOfDogs.size());
assertEquals("pluto", listOfDogs.get(0).getIdentifierValue());
}
}