Skip to content

Commit 9441a5e

Browse files
committed
refactor: Hoist builder setup out of assertThatThrownBy lambdas
SonarQube java:S5778 — each throwing lambda should contain only one invocation that could throw a runtime exception.
1 parent 6cb8a0b commit 9441a5e

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
66

77
import com.retailsvc.http.spec.Spec;
8+
import com.sun.net.httpserver.HttpHandler;
89
import java.util.List;
910
import java.util.Map;
1011
import org.junit.jupiter.api.Test;
@@ -32,39 +33,41 @@ void buildsWithRequiredFieldsOnly() {
3233

3334
@Test
3435
void rejectsDuplicateExtraPathOnSecondAddHandler() {
36+
HttpHandler duplicate = Handlers.aliveHandler();
3537
OpenApiServer.Builder b =
3638
OpenApiServer.builder()
3739
.spec(spec)
3840
.jsonMapper(jsonMapper)
3941
.handlers(emptyMap())
40-
.addHandler("/alive", Handlers.aliveHandler());
42+
.addHandler("/alive", duplicate);
4143

42-
assertThatThrownBy(() -> b.addHandler("/alive", Handlers.aliveHandler()))
44+
assertThatThrownBy(() -> b.addHandler("/alive", duplicate))
4345
.isInstanceOf(IllegalStateException.class)
4446
.hasMessageContaining("/alive");
4547
}
4648

4749
@Test
4850
void rejectsExtraPathEqualToSpecBasePathAtBuildTime() {
4951
// testSpec() uses "/api" as the basePath (servers[0].url = http://localhost:8080/api).
50-
assertThatThrownBy(
51-
() ->
52-
OpenApiServer.builder()
53-
.spec(spec)
54-
.jsonMapper(jsonMapper)
55-
.handlers(emptyMap())
56-
.addHandler("/api", Handlers.aliveHandler())
57-
.port(0)
58-
.build())
52+
OpenApiServer.Builder b =
53+
OpenApiServer.builder()
54+
.spec(spec)
55+
.jsonMapper(jsonMapper)
56+
.handlers(emptyMap())
57+
.addHandler("/api", Handlers.aliveHandler())
58+
.port(0);
59+
60+
assertThatThrownBy(b::build)
5961
.isInstanceOf(IllegalStateException.class)
6062
.hasMessageContaining("/api");
6163
}
6264

6365
@Test
6466
void rejectsNullSpec() {
65-
assertThatThrownBy(
66-
() ->
67-
OpenApiServer.builder().jsonMapper(jsonMapper).handlers(emptyMap()).port(0).build())
67+
OpenApiServer.Builder b =
68+
OpenApiServer.builder().jsonMapper(jsonMapper).handlers(emptyMap()).port(0);
69+
70+
assertThatThrownBy(b::build)
6871
.isInstanceOf(NullPointerException.class)
6972
.hasMessageContaining("Spec");
7073
}

0 commit comments

Comments
 (0)