Skip to content

Commit 1eefff2

Browse files
committed
test: Static-import Mockito.mock and assert chain invocation
Reduces visual noise at mock call sites and addresses three Sonar 'add at least one assertion' findings in the new coercion happy-path tests by verifying the downstream chain was invoked.
1 parent 2c2fcf4 commit 1eefff2

4 files changed

Lines changed: 37 additions & 22 deletions

File tree

src/test/java/com/retailsvc/http/HandlersDefaultExceptionTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.retailsvc.http;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.mockito.Mockito.mock;
45

56
import com.retailsvc.http.spec.HttpMethod;
67
import com.retailsvc.http.validate.ValidationError;
@@ -13,7 +14,7 @@
1314

1415
class HandlersDefaultExceptionTest {
1516
private HttpExchange newExchange(ByteArrayOutputStream sink) {
16-
HttpExchange ex = Mockito.mock(HttpExchange.class);
17+
HttpExchange ex = mock(HttpExchange.class);
1718
Mockito.when(ex.getResponseHeaders()).thenReturn(new Headers());
1819
Mockito.when(ex.getResponseBody()).thenReturn(sink);
1920
return ex;

src/test/java/com/retailsvc/http/internal/DispatchHandlerTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.retailsvc.http.internal;
22

33
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
import static org.mockito.Mockito.mock;
45

56
import com.retailsvc.http.MissingOperationHandlerException;
67
import com.retailsvc.http.Request;
@@ -20,8 +21,8 @@ private static void withOperationId(
2021

2122
@Test
2223
void invokesRegisteredHandler() throws Exception {
23-
HttpHandler handler = Mockito.mock(HttpHandler.class);
24-
HttpExchange ex = Mockito.mock(HttpExchange.class);
24+
HttpHandler handler = mock(HttpHandler.class);
25+
HttpExchange ex = mock(HttpExchange.class);
2526

2627
withOperationId(
2728
"get-x",
@@ -37,7 +38,7 @@ void invokesRegisteredHandler() throws Exception {
3738
@Test
3839
void throwsWhenHandlerMissing() {
3940
DispatchHandler d = new DispatchHandler(Map.of());
40-
HttpExchange ex = Mockito.mock(HttpExchange.class);
41+
HttpExchange ex = mock(HttpExchange.class);
4142

4243
assertThatThrownBy(
4344
() ->

src/test/java/com/retailsvc/http/internal/ExceptionFilterTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.retailsvc.http.internal;
22

3+
import static org.mockito.Mockito.mock;
4+
35
import com.retailsvc.http.ExceptionHandler;
46
import com.retailsvc.http.NotFoundException;
57
import com.sun.net.httpserver.Filter;
@@ -10,21 +12,21 @@
1012
class ExceptionFilterTest {
1113
@Test
1214
void delegatesToExceptionHandler() throws Exception {
13-
HttpExchange ex = Mockito.mock(HttpExchange.class);
14-
ExceptionHandler handler = Mockito.mock(ExceptionHandler.class);
15+
HttpExchange ex = mock(HttpExchange.class);
16+
ExceptionHandler handler = mock(ExceptionHandler.class);
1517
Filter f = new ExceptionFilter(handler);
16-
Filter.Chain chain = Mockito.mock(Filter.Chain.class);
18+
Filter.Chain chain = mock(Filter.Chain.class);
1719
Mockito.doThrow(new NotFoundException("x")).when(chain).doFilter(ex);
1820
f.doFilter(ex, chain);
1921
Mockito.verify(handler).handle(Mockito.eq(ex), Mockito.any(NotFoundException.class));
2022
}
2123

2224
@Test
2325
void passThroughOnSuccess() throws Exception {
24-
HttpExchange ex = Mockito.mock(HttpExchange.class);
25-
ExceptionHandler handler = Mockito.mock(ExceptionHandler.class);
26+
HttpExchange ex = mock(HttpExchange.class);
27+
ExceptionHandler handler = mock(ExceptionHandler.class);
2628
Filter f = new ExceptionFilter(handler);
27-
Filter.Chain chain = Mockito.mock(Filter.Chain.class);
29+
Filter.Chain chain = mock(Filter.Chain.class);
2830
f.doFilter(ex, chain);
2931
Mockito.verify(chain).doFilter(ex);
3032
Mockito.verifyNoInteractions(handler);

src/test/java/com/retailsvc/http/internal/RequestPreparationFilterTest.java

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
import static org.mockito.Mockito.mock;
56

67
import com.retailsvc.http.JsonMapper;
78
import com.retailsvc.http.MethodNotAllowedException;
@@ -37,7 +38,7 @@
3738
class RequestPreparationFilterTest {
3839

3940
private HttpExchange exchange(String method, String path, byte[] body) {
40-
HttpExchange ex = Mockito.mock(HttpExchange.class);
41+
HttpExchange ex = mock(HttpExchange.class);
4142
Mockito.when(ex.getRequestMethod()).thenReturn(method);
4243
Mockito.when(ex.getRequestURI()).thenReturn(URI.create(path));
4344
Mockito.when(ex.getRequestHeaders()).thenReturn(new Headers());
@@ -81,7 +82,7 @@ void successPathBindsRequestContextDuringChain() throws Exception {
8182
AtomicReference<String> seenOpId = new AtomicReference<>();
8283
AtomicReference<Map<String, String>> seenPathParams = new AtomicReference<>();
8384

84-
Filter.Chain chain = Mockito.mock(Filter.Chain.class);
85+
Filter.Chain chain = mock(Filter.Chain.class);
8586
Mockito.doAnswer(
8687
inv -> {
8788
seenOpId.set(Request.operationId());
@@ -112,7 +113,7 @@ void unknownPathThrowsNotFound() {
112113
Filter f = newFilter(spec);
113114

114115
HttpExchange ex = exchange("GET", "/missing", new byte[0]);
115-
assertThatThrownBy(() -> f.doFilter(ex, Mockito.mock(Filter.Chain.class)))
116+
assertThatThrownBy(() -> f.doFilter(ex, mock(Filter.Chain.class)))
116117
.isInstanceOf(NotFoundException.class);
117118
}
118119

@@ -130,7 +131,7 @@ void wrongMethodThrowsMethodNotAllowed() {
130131
Filter f = newFilter(spec);
131132

132133
HttpExchange ex = exchange("POST", "/x", new byte[0]);
133-
assertThatThrownBy(() -> f.doFilter(ex, Mockito.mock(Filter.Chain.class)))
134+
assertThatThrownBy(() -> f.doFilter(ex, mock(Filter.Chain.class)))
134135
.isInstanceOf(MethodNotAllowedException.class);
135136
}
136137

@@ -149,7 +150,7 @@ void invalidQueryParamThrowsValidation() {
149150
Filter f = newFilter(spec);
150151

151152
HttpExchange ex = exchange("GET", "/x?q=ab", new byte[0]);
152-
assertThatThrownBy(() -> f.doFilter(ex, Mockito.mock(Filter.Chain.class)))
153+
assertThatThrownBy(() -> f.doFilter(ex, mock(Filter.Chain.class)))
153154
.isInstanceOf(ValidationException.class)
154155
.extracting(t -> ((ValidationException) t).error().pointer())
155156
.isEqualTo("/query/q");
@@ -170,7 +171,9 @@ void integerQueryParamIsCoercedFromStringBeforeValidation() throws Exception {
170171
Filter f = newFilter(spec);
171172

172173
HttpExchange ex = exchange("GET", "/x?n=42", new byte[0]);
173-
f.doFilter(ex, Mockito.mock(Filter.Chain.class));
174+
Filter.Chain chain = mock(Filter.Chain.class);
175+
f.doFilter(ex, chain);
176+
Mockito.verify(chain).doFilter(ex);
174177
}
175178

176179
@Test
@@ -188,7 +191,7 @@ void integerQueryParamRejectsNonNumericString() {
188191
Filter f = newFilter(spec);
189192

190193
HttpExchange ex = exchange("GET", "/x?n=abc", new byte[0]);
191-
assertThatThrownBy(() -> f.doFilter(ex, Mockito.mock(Filter.Chain.class)))
194+
assertThatThrownBy(() -> f.doFilter(ex, mock(Filter.Chain.class)))
192195
.isInstanceOf(ValidationException.class)
193196
.extracting(t -> ((ValidationException) t).error().keyword())
194197
.isEqualTo("type");
@@ -209,7 +212,9 @@ void numberQueryParamIsCoercedFromStringBeforeValidation() throws Exception {
209212
Filter f = newFilter(spec);
210213

211214
HttpExchange ex = exchange("GET", "/x?n=1.5", new byte[0]);
212-
f.doFilter(ex, Mockito.mock(Filter.Chain.class));
215+
Filter.Chain chain = mock(Filter.Chain.class);
216+
f.doFilter(ex, chain);
217+
Mockito.verify(chain).doFilter(ex);
213218
}
214219

215220
@Test
@@ -227,7 +232,7 @@ void numberQueryParamRejectsNonNumericString() {
227232
Filter f = newFilter(spec);
228233

229234
HttpExchange ex = exchange("GET", "/x?n=abc", new byte[0]);
230-
assertThatThrownBy(() -> f.doFilter(ex, Mockito.mock(Filter.Chain.class)))
235+
assertThatThrownBy(() -> f.doFilter(ex, mock(Filter.Chain.class)))
231236
.isInstanceOf(ValidationException.class)
232237
.extracting(t -> ((ValidationException) t).error().keyword())
233238
.isEqualTo("type");
@@ -247,8 +252,14 @@ void booleanQueryParamCoercesTrueAndFalse() throws Exception {
247252
Spec spec = specWith(op);
248253
Filter f = newFilter(spec);
249254

250-
f.doFilter(exchange("GET", "/x?b=true", new byte[0]), Mockito.mock(Filter.Chain.class));
251-
f.doFilter(exchange("GET", "/x?b=false", new byte[0]), Mockito.mock(Filter.Chain.class));
255+
Filter.Chain trueChain = mock(Filter.Chain.class);
256+
Filter.Chain falseChain = mock(Filter.Chain.class);
257+
HttpExchange trueEx = exchange("GET", "/x?b=true", new byte[0]);
258+
HttpExchange falseEx = exchange("GET", "/x?b=false", new byte[0]);
259+
f.doFilter(trueEx, trueChain);
260+
f.doFilter(falseEx, falseChain);
261+
Mockito.verify(trueChain).doFilter(trueEx);
262+
Mockito.verify(falseChain).doFilter(falseEx);
252263
}
253264

254265
@Test
@@ -266,7 +277,7 @@ void booleanQueryParamRejectsNonBooleanString() {
266277
Filter f = newFilter(spec);
267278

268279
HttpExchange ex = exchange("GET", "/x?b=yes", new byte[0]);
269-
assertThatThrownBy(() -> f.doFilter(ex, Mockito.mock(Filter.Chain.class)))
280+
assertThatThrownBy(() -> f.doFilter(ex, mock(Filter.Chain.class)))
270281
.isInstanceOf(ValidationException.class)
271282
.extracting(t -> ((ValidationException) t).error().keyword())
272283
.isEqualTo("type");

0 commit comments

Comments
 (0)