Skip to content

Commit dd6f5eb

Browse files
committed
Mods to use concrete FakeResponse instead of a Mockito Spy FakeResponse (#370).
1 parent 40ec8d2 commit dd6f5eb

8 files changed

+150
-36
lines changed

src/test/java/org/gitlab4j/api/FakeResponse.java

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,31 @@
11
package org.gitlab4j.api;
22

33
import java.io.ByteArrayInputStream;
4+
import java.lang.annotation.Annotation;
5+
import java.net.URI;
6+
import java.util.Date;
47
import java.util.List;
8+
import java.util.Locale;
9+
import java.util.Map;
10+
import java.util.Set;
511

12+
import javax.ws.rs.core.EntityTag;
613
import javax.ws.rs.core.GenericType;
14+
import javax.ws.rs.core.Link;
15+
import javax.ws.rs.core.Link.Builder;
16+
import javax.ws.rs.core.MediaType;
17+
import javax.ws.rs.core.MultivaluedMap;
18+
import javax.ws.rs.core.NewCookie;
719
import javax.ws.rs.core.Response;
820

921
/**
10-
* This class can be used as a spy for Mockito to test the individual APIs
22+
* This class can be used as a concrete mock to test the individual APIs
1123
* getXxxxx() methods without the need to have a GitLab server available.
1224
*
1325
* Supports getXxxxx() methods that return a List of items, single items,
1426
* Optional items, and Pagers of items.
1527
*/
16-
public abstract class FakeResponse extends Response {
28+
public class FakeResponse extends Response {
1729

1830
private List<?> responseList;
1931
private Object responseItem;
@@ -51,8 +63,10 @@ public void setPerPageHeaderValue(int perPage) {
5163
this.perPage = perPage;
5264
}
5365

54-
// The below methods allow this abstract class to act as a fake Resource
55-
// instance.
66+
67+
/******************************************************************************
68+
* The following methods allow this class to act as a fake Resource instance.
69+
******************************************************************************/
5670

5771
@SuppressWarnings("unchecked")
5872
@Override
@@ -93,4 +107,115 @@ public String getHeaderString(String name) {
93107
public int getStatus() {
94108
return (200);
95109
}
110+
111+
112+
/**************************************************************************************************
113+
* The remaining methods are stubbed so we can create an instance of this class. They are simply
114+
* stubs, but needed to do this because the Mockito Spy annotation does not work without JAXB
115+
* on Java 11+ and did not wish to pull in the JAXB module even for testing.
116+
**************************************************************************************************/
117+
118+
@Override
119+
public StatusType getStatusInfo() {
120+
return null;
121+
}
122+
123+
@Override
124+
public <T> T readEntity(Class<T> entityType, Annotation[] annotations) {
125+
return null;
126+
}
127+
128+
@Override
129+
public <T> T readEntity(GenericType<T> entityType, Annotation[] annotations) {
130+
return null;
131+
}
132+
133+
@Override
134+
public boolean hasEntity() {
135+
return false;
136+
}
137+
138+
@Override
139+
public boolean bufferEntity() {
140+
return false;
141+
}
142+
143+
@Override
144+
public void close() {
145+
}
146+
147+
@Override
148+
public MediaType getMediaType() {
149+
return null;
150+
}
151+
152+
@Override
153+
public Locale getLanguage() {
154+
return null;
155+
}
156+
157+
@Override
158+
public int getLength() {
159+
return 0;
160+
}
161+
162+
@Override
163+
public Set<String> getAllowedMethods() {
164+
return null;
165+
}
166+
167+
@Override
168+
public Map<String, NewCookie> getCookies() {
169+
return null;
170+
}
171+
172+
@Override
173+
public EntityTag getEntityTag() {
174+
return null;
175+
}
176+
177+
@Override
178+
public Date getDate() {
179+
return null;
180+
}
181+
182+
@Override
183+
public Date getLastModified() {
184+
return null;
185+
}
186+
187+
@Override
188+
public URI getLocation() {
189+
return null;
190+
}
191+
192+
@Override
193+
public Set<Link> getLinks() {
194+
return null;
195+
}
196+
197+
@Override
198+
public boolean hasLink(String relation) {
199+
return false;
200+
}
201+
202+
@Override
203+
public Link getLink(String relation) {
204+
return null;
205+
}
206+
207+
@Override
208+
public Builder getLinkBuilder(String relation) {
209+
return null;
210+
}
211+
212+
@Override
213+
public MultivaluedMap<String, Object> getMetadata() {
214+
return null;
215+
}
216+
217+
@Override
218+
public MultivaluedMap<String, String> getStringHeaders() {
219+
return null;
220+
}
96221
}

src/test/java/org/gitlab4j/api/TestCommitDiscussionsApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
import org.mockito.Captor;
2121
import org.mockito.Mock;
2222
import org.mockito.Mockito;
23-
import org.mockito.Spy;
2423

2524
public class TestCommitDiscussionsApi implements Constants {
2625

2726
private static final String COMMIT_SHA = "abcdef1234567890";
2827
@Mock private GitLabApi gitLabApi;
2928
@Mock private GitLabApiClient gitLabApiClient;
30-
@Spy private FakeResponse response;
3129
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
30+
private FakeResponse response = new FakeResponse();
3231

3332
@Before
3433
public void setUp() throws Exception {

src/test/java/org/gitlab4j/api/TestEpicDiscussionsApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import org.mockito.Captor;
2121
import org.mockito.Mock;
2222
import org.mockito.Mockito;
23-
import org.mockito.Spy;
2423

2524
public class TestEpicDiscussionsApi implements Constants {
2625

2726
@Mock private GitLabApi gitLabApi;
2827
@Mock private GitLabApiClient gitLabApiClient;
29-
@Spy private FakeResponse response;
3028
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
29+
private FakeResponse response = new FakeResponse();
3130

3231
@Before
3332
public void setUp() throws Exception {

src/test/java/org/gitlab4j/api/TestIssueDiscussionsApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import org.mockito.Captor;
2121
import org.mockito.Mock;
2222
import org.mockito.Mockito;
23-
import org.mockito.Spy;
2423

2524
public class TestIssueDiscussionsApi implements Constants {
2625

2726
@Mock private GitLabApi gitLabApi;
2827
@Mock private GitLabApiClient gitLabApiClient;
29-
@Spy private FakeResponse response;
3028
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
29+
private FakeResponse response = new FakeResponse();
3130

3231
@Before
3332
public void setUp() throws Exception {

src/test/java/org/gitlab4j/api/TestMergeRequestApi.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
package org.gitlab4j.api;
22

3+
import static org.hamcrest.Matchers.hasEntry;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertThat;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.Mockito.when;
8+
import static org.mockito.MockitoAnnotations.initMocks;
9+
10+
import java.util.Collections;
11+
12+
import javax.ws.rs.core.MultivaluedMap;
13+
314
import org.gitlab4j.api.models.MergeRequest;
415
import org.junit.Before;
516
import org.junit.Test;
@@ -8,38 +19,23 @@
819
import org.mockito.Mock;
920
import org.mockito.Mockito;
1021

11-
import javax.ws.rs.core.MultivaluedMap;
12-
import javax.ws.rs.core.Response;
13-
import java.util.Collections;
14-
15-
import static org.hamcrest.Matchers.hasEntry;
16-
import static org.junit.Assert.assertEquals;
17-
import static org.junit.Assert.assertThat;
18-
import static org.mockito.Matchers.any;
19-
import static org.mockito.Mockito.when;
20-
import static org.mockito.MockitoAnnotations.initMocks;
21-
2222
public class TestMergeRequestApi {
2323

2424
@Mock private GitLabApi gitLabApi;
2525
@Mock private GitLabApiClient gitLabApiClient;
26-
@Mock private Response response;
27-
2826
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
27+
private FakeResponse response = new FakeResponse();
2928

3029
@SuppressWarnings("deprecation")
3130
@Before
3231
public void setUp() throws Exception {
3332
initMocks(this);
34-
33+
response.init(MergeRequest.class, "merge-request.json", null);
3534
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
3635

3736
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
3837
when(gitLabApiClient.put(attributeCaptor.capture(), Mockito.<Object>anyVararg()))
3938
.thenReturn(response);
40-
41-
when(response.getStatus()).thenReturn(200);
42-
when(response.getEntity()).thenReturn(new MergeRequest());
4339
}
4440

4541
@Test

src/test/java/org/gitlab4j/api/TestMergeRequestDiscussionsApi.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@
2020
import org.mockito.Captor;
2121
import org.mockito.Mock;
2222
import org.mockito.Mockito;
23-
import org.mockito.Spy;
2423

2524
public class TestMergeRequestDiscussionsApi implements Constants {
2625

2726
@Mock private GitLabApi gitLabApi;
2827
@Mock private GitLabApiClient gitLabApiClient;
29-
@Spy private FakeResponse response;
3028
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
29+
private FakeResponse response = new FakeResponse();
3130

3231
@Before
3332
public void setUp() throws Exception {
3433
initMocks(this);
35-
response.init(Discussion.class, null, "merge-request-discussions.json");
34+
response.init(Discussion.class, null, "merge-request-discussions.json");
3635
when(gitLabApi.getApiClient()).thenReturn(gitLabApiClient);
3736
when(gitLabApiClient.validateSecretToken(any())).thenReturn(true);
3837
when(gitLabApiClient.get(attributeCaptor.capture(), Mockito.<Object>any())).thenReturn(response);

src/test/java/org/gitlab4j/api/TestSnippetDiscussionsApi.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
import org.mockito.Captor;
2121
import org.mockito.Mock;
2222
import org.mockito.Mockito;
23-
import org.mockito.Spy;
2423

2524
public class TestSnippetDiscussionsApi implements Constants {
2625

2726
@Mock private GitLabApi gitLabApi;
2827
@Mock private GitLabApiClient gitLabApiClient;
29-
@Spy private FakeResponse response;
3028
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
29+
private FakeResponse response = new FakeResponse();
3130

3231
@Before
3332
public void setUp() throws Exception {

src/test/java/org/gitlab4j/api/TestStreams.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import static java.util.Comparator.comparing;
4+
import static java.util.stream.Collectors.toList;
45
import static org.gitlab4j.api.JsonUtils.compareJson;
56
import static org.junit.Assert.assertEquals;
67
import static org.junit.Assert.assertNotNull;
@@ -10,8 +11,6 @@
1011
import static org.mockito.Mockito.when;
1112
import static org.mockito.MockitoAnnotations.initMocks;
1213

13-
import static java.util.stream.Collectors.toList;
14-
1514
import java.util.List;
1615
import java.util.stream.Stream;
1716

@@ -25,14 +24,13 @@
2524
import org.mockito.Captor;
2625
import org.mockito.Mock;
2726
import org.mockito.Mockito;
28-
import org.mockito.Spy;
2927

3028
public class TestStreams implements Constants {
3129

3230
@Mock private GitLabApi gitLabApi;
3331
@Mock private GitLabApiClient gitLabApiClient;
34-
@Spy private FakeResponse response;
3532
@Captor private ArgumentCaptor<MultivaluedMap<String, String>> attributeCaptor;
33+
private FakeResponse response = new FakeResponse();
3634

3735
static private List<User> sortedUsers;
3836

0 commit comments

Comments
 (0)