Skip to content

Commit f2269cf

Browse files
committed
fix(test): Hoist schema construction out of assertThatThrownBy lambdas
Sonar java:S5778 wants exactly one possibly-throwing invocation inside the lambda passed to assertThatThrownBy, so it can attribute the thrown exception unambiguously. Extract the schema constructor (or helper call) to a local before the assertion. Affects 7 occurrences across DefaultValidatorDispatchTest and ValidatorNoThrowOnHappyPathTest.
1 parent e41d0d7 commit f2269cf

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

src/test/java/com/retailsvc/http/validate/DefaultValidatorDispatchTest.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,17 @@ void allOfWithEmptyPartsAlwaysPasses() {
144144

145145
@Test
146146
void anyOfWithEmptyOptionsAlwaysFails() {
147-
assertThatThrownBy(() -> v.validate("anything", new AnyOfSchema(List.of(), Map.of()), "/v"))
147+
var schema = new AnyOfSchema(List.of(), Map.of());
148+
assertThatThrownBy(() -> v.validate("anything", schema, "/v"))
148149
.isInstanceOf(ValidationException.class)
149150
.extracting(t -> ((ValidationException) t).error().keyword())
150151
.isEqualTo("anyOf");
151152
}
152153

153154
@Test
154155
void oneOfWithEmptyOptionsAlwaysFails() {
155-
assertThatThrownBy(() -> v.validate("anything", new OneOfSchema(List.of(), Map.of()), "/v"))
156+
var schema = new OneOfSchema(List.of(), Map.of());
157+
assertThatThrownBy(() -> v.validate("anything", schema, "/v"))
156158
.isInstanceOf(ValidationException.class)
157159
.satisfies(
158160
t -> {
@@ -165,8 +167,8 @@ void oneOfWithEmptyOptionsAlwaysFails() {
165167
@Test
166168
void notWithNullSchemaRejectsNull() {
167169
// not(NullSchema) — inner accepts null, outer must reject.
168-
assertThatThrownBy(
169-
() -> v.validate(null, new NotSchema(new NullSchema(Map.of()), Map.of()), "/v"))
170+
var schema = new NotSchema(new NullSchema(Map.of()), Map.of());
171+
assertThatThrownBy(() -> v.validate(null, schema, "/v"))
170172
.isInstanceOf(ValidationException.class)
171173
.extracting(t -> ((ValidationException) t).error().keyword())
172174
.isEqualTo("not");
@@ -202,7 +204,8 @@ void alwaysSchemaAcceptsNull() {
202204

203205
@Test
204206
void neverSchemaRejectsString() {
205-
assertThatThrownBy(() -> v.validate("anything", new NeverSchema(Map.of()), "/v"))
207+
var schema = new NeverSchema(Map.of());
208+
assertThatThrownBy(() -> v.validate("anything", schema, "/v"))
206209
.isInstanceOf(ValidationException.class)
207210
.satisfies(
208211
t -> {
@@ -217,15 +220,17 @@ void neverSchemaRejectsString() {
217220
// Full ValidationError surface is verified by neverSchemaRejectsString; these cover keyword only.
218221
@Test
219222
void neverSchemaRejectsInteger() {
220-
assertThatThrownBy(() -> v.validate(42, new NeverSchema(Map.of()), "/v"))
223+
var schema = new NeverSchema(Map.of());
224+
assertThatThrownBy(() -> v.validate(42, schema, "/v"))
221225
.isInstanceOf(ValidationException.class)
222226
.extracting(t -> ((ValidationException) t).error().keyword())
223227
.isEqualTo("false");
224228
}
225229

226230
@Test
227231
void neverSchemaRejectsNull() {
228-
assertThatThrownBy(() -> v.validate(null, new NeverSchema(Map.of()), "/v"))
232+
var schema = new NeverSchema(Map.of());
233+
assertThatThrownBy(() -> v.validate(null, schema, "/v"))
229234
.isInstanceOf(ValidationException.class)
230235
.extracting(t -> ((ValidationException) t).error().keyword())
231236
.isEqualTo("false");

src/test/java/com/retailsvc/http/validate/ValidatorNoThrowOnHappyPathTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ void successfulAnyOfDoesNotConstructValidationException() {
8888
@Test
8989
void failingOneOfConstructsExactlyOneValidationException() {
9090
long before = ValidationException.CONSTRUCTIONS.get();
91+
var schema = stringOrNumber();
9192

92-
assertThatThrownBy(() -> validator.validate(true, stringOrNumber(), "/v"))
93+
assertThatThrownBy(() -> validator.validate(true, schema, "/v"))
9394
.isInstanceOf(ValidationException.class)
9495
.extracting(t -> ((ValidationException) t).error().keyword())
9596
.isEqualTo("oneOf");

0 commit comments

Comments
 (0)