Skip to content

Commit 7898c56

Browse files
committed
Revise "Use PreconditionAssertions wherever feasible"
This commit picks up where the previous commit left off. See #4943 See #5019
1 parent 1312cda commit 7898c56

23 files changed

+242
-239
lines changed

jupiter-tests/src/test/java/org/junit/jupiter/params/provider/CsvFileArgumentsProviderTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,9 +476,8 @@ void throwsExceptionWhenMaxCharsPerColumnIsNotPositiveNumberOrMinusOne(int maxCh
476476
.files(csvFile.toAbsolutePath().toString())//
477477
.build();
478478

479-
assertPreconditionViolationFor(
480-
() -> provideArguments(new CsvFileArgumentsProvider(), annotation).findAny()).withMessageStartingWith(
481-
"maxCharsPerColumn must be a positive number or -1: " + maxCharsPerColumn);
479+
assertPreconditionViolationFor(() -> provideArguments(new CsvFileArgumentsProvider(), annotation).findAny())//
480+
.withMessageStartingWith("maxCharsPerColumn must be a positive number or -1: " + maxCharsPerColumn);
482481
}
483482

484483
@Test

jupiter-tests/src/test/java/org/junit/jupiter/params/provider/EnumArgumentsProviderTests.java

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
package org.junit.jupiter.params.provider;
1212

1313
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
14+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
1515
import static org.junit.jupiter.params.provider.EnumArgumentsProviderTests.EnumWithFourConstants.BAR;
1616
import static org.junit.jupiter.params.provider.EnumArgumentsProviderTests.EnumWithFourConstants.BAZ;
1717
import static org.junit.jupiter.params.provider.EnumArgumentsProviderTests.EnumWithFourConstants.FOO;
@@ -63,21 +63,21 @@ void provideAllEnumConstantsWithNamingAll() {
6363
@Test
6464
void duplicateConstantNameIsDetected() {
6565
assertPreconditionViolationFor(
66-
() -> provideArguments(EnumWithFourConstants.class, "FOO", "BAR", "FOO").findAny()).withMessageContaining(
67-
"Duplicate enum constant name(s) found");
66+
() -> provideArguments(EnumWithFourConstants.class, "FOO", "BAR", "FOO").findAny())//
67+
.withMessageContaining("Duplicate enum constant name(s) found");
6868
}
6969

7070
@Test
7171
void invalidConstantNameIsDetected() {
72-
assertPreconditionViolationFor(
73-
() -> provideArguments(EnumWithFourConstants.class, "FO0", "B4R").findAny()).withMessageContaining(
74-
"Invalid enum constant name(s) in");
72+
assertPreconditionViolationFor(() -> provideArguments(EnumWithFourConstants.class, "FO0", "B4R").findAny())//
73+
.withMessageContaining("Invalid enum constant name(s) in");
7574
}
7675

7776
@Test
7877
void invalidPatternIsDetected() {
79-
assertPreconditionViolationFor(() -> provideArguments(EnumWithFourConstants.class, Mode.MATCH_ALL, "(",
80-
")").findAny()).withMessageContaining("Pattern compilation failed");
78+
assertPreconditionViolationFor(
79+
() -> provideArguments(EnumWithFourConstants.class, Mode.MATCH_ALL, "(", ")").findAny())//
80+
.withMessageContaining("Pattern compilation failed");
8181
}
8282

8383
@Test
@@ -98,16 +98,16 @@ void incorrectParameterTypeIsDetected() {
9898
when(firstParameterDeclaration.getParameterType()).thenAnswer(__ -> Object.class);
9999
when(parameters.getFirst()).thenReturn(Optional.of(firstParameterDeclaration));
100100

101-
assertPreconditionViolationFor(() -> provideArguments(NullEnum.class).findAny()).withMessageStartingWith(
102-
"First parameter must reference an Enum type");
101+
assertPreconditionViolationFor(() -> provideArguments(NullEnum.class).findAny())//
102+
.withMessageStartingWith("First parameter must reference an Enum type");
103103
}
104104

105105
@Test
106106
void methodsWithoutParametersAreDetected() {
107107
when(parameters.getSourceElementDescription()).thenReturn("method");
108108

109-
assertPreconditionViolationFor(() -> provideArguments(NullEnum.class).findAny()).withMessageStartingWith(
110-
"There must be at least one declared parameter for method");
109+
assertPreconditionViolationFor(() -> provideArguments(NullEnum.class).findAny())//
110+
.withMessageStartingWith("There must be at least one declared parameter for method");
111111
}
112112

113113
@Test
@@ -147,34 +147,37 @@ void providesNoEnumConstant() {
147147

148148
@Test
149149
void invalidConstantNameIsDetectedInRange() {
150-
assertPreconditionViolationFor(() -> provideArguments(EnumWithFourConstants.class, "FOO", "BAZ", Mode.EXCLUDE,
151-
"QUX").findAny()).withMessageContaining("Invalid enum constant name(s) in");
150+
assertPreconditionViolationFor(
151+
() -> provideArguments(EnumWithFourConstants.class, "FOO", "BAZ", Mode.EXCLUDE, "QUX").findAny())//
152+
.withMessageContaining("Invalid enum constant name(s) in");
152153
}
153154

154155
@Test
155156
void invalidStartingRangeIsDetected() {
156-
var exception = assertThrows(IllegalArgumentException.class,
157-
() -> provideArguments(EnumWithFourConstants.class, "B4R", "", Mode.INCLUDE).findAny());
158-
assertThat(exception).hasMessageContaining("No enum constant");
157+
assertThatIllegalArgumentException()//
158+
.isThrownBy(() -> provideArguments(EnumWithFourConstants.class, "B4R", "", Mode.INCLUDE).findAny())//
159+
.withMessageContaining("No enum constant");
159160
}
160161

161162
@Test
162163
void invalidEndingRangeIsDetected() {
163-
var exception = assertThrows(IllegalArgumentException.class,
164-
() -> provideArguments(EnumWithFourConstants.class, "", "B4R", Mode.INCLUDE).findAny());
165-
assertThat(exception).hasMessageContaining("No enum constant");
164+
assertThatIllegalArgumentException()//
165+
.isThrownBy(() -> provideArguments(EnumWithFourConstants.class, "", "B4R", Mode.INCLUDE).findAny())//
166+
.withMessageContaining("No enum constant");
166167
}
167168

168169
@Test
169170
void invalidRangeOrderIsDetected() {
170-
assertPreconditionViolationFor(() -> provideArguments(EnumWithFourConstants.class, "BAR", "FOO",
171-
Mode.INCLUDE).findAny()).withMessageContaining("Invalid enum range");
171+
assertPreconditionViolationFor(
172+
() -> provideArguments(EnumWithFourConstants.class, "BAR", "FOO", Mode.INCLUDE).findAny())//
173+
.withMessageContaining("Invalid enum range");
172174
}
173175

174176
@Test
175177
void invalidRangeIsDetectedWhenEnumWithNoConstantIsProvided() {
176-
assertPreconditionViolationFor(() -> provideArguments(EnumWithNoConstant.class, "BAR", "FOO",
177-
Mode.INCLUDE).findAny()).withMessageContaining("No enum constant");
178+
assertPreconditionViolationFor(
179+
() -> provideArguments(EnumWithNoConstant.class, "BAR", "FOO", Mode.INCLUDE).findAny())//
180+
.withMessageContaining("No enum constant");
178181
}
179182

180183
static class TestCase {

jupiter-tests/src/test/java/org/junit/jupiter/params/provider/ValueArgumentsProviderTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ class ValueArgumentsProviderTests {
2727

2828
@Test
2929
void multipleInputsAreNotAllowed() {
30-
assertPreconditionViolationFor(
31-
() -> provideArguments(new short[1], new byte[0], new int[1], new long[0], new float[0], new double[0],
32-
new char[0], new boolean[0], new String[0], new Class<?>[0]).findAny()).withMessageContaining(
33-
"Exactly one type of input must be provided in the @ValueSource annotation, but there were 2");
30+
assertPreconditionViolationFor(() -> provideArguments(new short[1], new byte[0], new int[1], new long[0],
31+
new float[0], new double[0], new char[0], new boolean[0], new String[0], new Class<?>[0]).findAny())//
32+
.withMessageContaining(
33+
"Exactly one type of input must be provided in the @ValueSource annotation, but there were 2");
3434
}
3535

3636
@Test
3737
void onlyEmptyInputsAreNotAllowed() {
38-
assertPreconditionViolationFor(
39-
() -> provideArguments(new short[0], new byte[0], new int[0], new long[0], new float[0], new double[0],
40-
new char[0], new boolean[0], new String[0], new Class<?>[0]).findAny()).withMessageContaining(
41-
"Exactly one type of input must be provided in the @ValueSource annotation, but there were 0");
38+
assertPreconditionViolationFor(() -> provideArguments(new short[0], new byte[0], new int[0], new long[0],
39+
new float[0], new double[0], new char[0], new boolean[0], new String[0], new Class<?>[0]).findAny())//
40+
.withMessageContaining(
41+
"Exactly one type of input must be provided in the @ValueSource annotation, but there were 0");
4242
}
4343

4444
/**

platform-tests/src/test/java/org/junit/platform/commons/support/ReflectionSupportTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,8 +382,8 @@ void invokeMethodPreconditions() throws Exception {
382382
assertPreconditionViolationNotNullFor("Method", () -> ReflectionSupport.invokeMethod(null, null, "true"));
383383

384384
var method = Boolean.class.getMethod("toString");
385-
assertPreconditionViolationFor(() -> ReflectionSupport.invokeMethod(method, null)).withMessage(
386-
"Cannot invoke non-static method [" + method.toGenericString() + "] on a null target.");
385+
assertPreconditionViolationFor(() -> ReflectionSupport.invokeMethod(method, null))//
386+
.withMessage("Cannot invoke non-static method [" + method.toGenericString() + "] on a null target.");
387387
}
388388

389389
@Test
@@ -430,9 +430,9 @@ void tryToReadFieldValuePreconditions() throws Exception {
430430
assertPreconditionViolationNotNullFor("Field", () -> ReflectionSupport.tryToReadFieldValue(null, this));
431431

432432
var instanceField = getClass().getDeclaredField("instanceField");
433-
assertPreconditionViolationFor(
434-
() -> ReflectionSupport.tryToReadFieldValue(instanceField, null)).withMessageStartingWith(
435-
"Cannot read non-static field").withMessageEndingWith("on a null instance.");
433+
assertPreconditionViolationFor(() -> ReflectionSupport.tryToReadFieldValue(instanceField, null))//
434+
.withMessageStartingWith("Cannot read non-static field")//
435+
.withMessageEndingWith("on a null instance.");
436436
}
437437

438438
@Test

platform-tests/src/test/java/org/junit/platform/commons/util/AnnotationUtilsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ void isAnnotatedWhenMetaPresentOnMethod() throws Exception {
209209

210210
@Test
211211
void findRepeatableAnnotationsForNotRepeatableAnnotation() {
212-
assertPreconditionViolationFor(() -> findRepeatableAnnotations(getClass(), Inherited.class)).withMessage(
213-
Inherited.class.getName() + " must be @Repeatable");
212+
assertPreconditionViolationFor(() -> findRepeatableAnnotations(getClass(), Inherited.class))//
213+
.withMessage(Inherited.class.getName() + " must be @Repeatable");
214214
}
215215

216216
@Test

platform-tests/src/test/java/org/junit/platform/commons/util/CollectionUtilsTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ void nullCollection() {
6565

6666
@Test
6767
void emptyCollection() {
68-
assertPreconditionViolationFor(() -> CollectionUtils.getOnlyElement(Set.of())).withMessage(
69-
"collection must contain exactly one element: []");
68+
assertPreconditionViolationFor(() -> CollectionUtils.getOnlyElement(Set.of()))//
69+
.withMessage("collection must contain exactly one element: []");
7070
}
7171

7272
@Test
@@ -78,8 +78,8 @@ void singleElementCollection() {
7878

7979
@Test
8080
void multiElementCollection() {
81-
assertPreconditionViolationFor(() -> CollectionUtils.getOnlyElement(List.of("foo", "bar"))).withMessage(
82-
"collection must contain exactly one element: [foo, bar]");
81+
assertPreconditionViolationFor(() -> CollectionUtils.getOnlyElement(List.of("foo", "bar")))//
82+
.withMessage("collection must contain exactly one element: [foo, bar]");
8383
}
8484
}
8585

@@ -185,8 +185,8 @@ void toStreamWithNull() {
185185

186186
@Test
187187
void toStreamWithUnsupportedObjectType() {
188-
assertPreconditionViolationFor(() -> CollectionUtils.toStream("unknown")).withMessage(
189-
"Cannot convert instance of java.lang.String into a Stream: unknown");
188+
assertPreconditionViolationFor(() -> CollectionUtils.toStream("unknown"))//
189+
.withMessage("Cannot convert instance of java.lang.String into a Stream: unknown");
190190
}
191191

192192
@Test
@@ -281,9 +281,9 @@ void toStreamWithIteratorProvider() {
281281
@Test
282282
void throwWhenIteratorNamedMethodDoesNotReturnAnIterator() {
283283
var o = new UnusableIteratorProvider("Test");
284-
assertPreconditionViolationFor(() -> CollectionUtils.toStream(o)).withMessage(
285-
"Cannot convert instance of %s into a Stream: %s".formatted(UnusableIteratorProvider.class.getName(),
286-
o));
284+
assertPreconditionViolationFor(() -> CollectionUtils.toStream(o))//
285+
.withMessage("Cannot convert instance of %s into a Stream: %s".formatted(
286+
UnusableIteratorProvider.class.getName(), o));
287287
}
288288

289289
@Test

platform-tests/src/test/java/org/junit/platform/commons/util/ReflectionUtilsTests.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,13 +1294,13 @@ void findMethodByParameterTypesPreconditions() {
12941294

12951295
assertPreconditionViolationNotNullOrBlankFor("Method name", () -> findMethod(String.class, " "));
12961296

1297-
assertPreconditionViolationNotNullFor("Parameter types array",//
1297+
assertPreconditionViolationNotNullFor("Parameter types array",
12981298
() -> findMethod(Files.class, "copy", (Class<?>[]) null));
12991299

1300-
assertPreconditionViolationNotNullFor("Individual parameter types",//
1300+
assertPreconditionViolationNotNullFor("Individual parameter types",
13011301
() -> findMethod(Files.class, "copy", (Class<?>) null));
13021302

1303-
assertPreconditionViolationNotNullFor("Individual parameter types",//
1303+
assertPreconditionViolationNotNullFor("Individual parameter types",
13041304
() -> findMethod(Files.class, "copy", new Class<?>[] { Path.class, null }));
13051305
// @formatter:on
13061306
}
@@ -1910,8 +1910,9 @@ void tryToReadFieldValueOfExistingInstanceField() throws Exception {
19101910
var field = MyClass.class.getDeclaredField("instanceField");
19111911
assertThat(tryToReadFieldValue(field, instance).getNonNull()).isEqualTo(42);
19121912

1913-
assertPreconditionViolationFor(() -> tryToReadFieldValue(field, null).get()).withMessageStartingWith(
1914-
"Cannot read non-static field").withMessageEndingWith("on a null instance.");
1913+
assertPreconditionViolationFor(() -> tryToReadFieldValue(field, null).get())//
1914+
.withMessageStartingWith("Cannot read non-static field")//
1915+
.withMessageEndingWith("on a null instance.");
19151916
}
19161917

19171918
}

platform-tests/src/test/java/org/junit/platform/engine/TestTagTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ private static void nope(String tag) {
9797
}
9898

9999
private void assertSyntaxViolation(String tag) {
100-
assertPreconditionViolationFor(() -> TestTag.create(tag)).withMessageStartingWith(
101-
"Tag name").withMessageEndingWith("must be syntactically valid");
100+
assertPreconditionViolationFor(() -> TestTag.create(tag))//
101+
.withMessageStartingWith("Tag name")//
102+
.withMessageEndingWith("must be syntactically valid");
102103
}
103104

104105
}

platform-tests/src/test/java/org/junit/platform/engine/UniqueIdTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ void removesLastSegment() {
292292

293293
}
294294

295-
private void assertSegment(Segment segment, String expectedType, String expectedValue) {
295+
private static void assertSegment(Segment segment, String expectedType, String expectedValue) {
296296
assertEquals(expectedType, segment.getType(), "segment type");
297297
assertEquals(expectedValue, segment.getValue(), "segment value");
298298
}

platform-tests/src/test/java/org/junit/platform/engine/discovery/ClassNameFilterTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ void includeClassNamePatternsChecksPreconditions() {
3030
() -> ClassNameFilter.includeClassNamePatterns((String[]) null));
3131
assertPreconditionViolationNotNullOrEmptyFor("patterns array",
3232
() -> ClassNameFilter.includeClassNamePatterns(new String[0]));
33-
assertPreconditionViolationFor(
34-
() -> ClassNameFilter.includeClassNamePatterns(new String[] { null })).withMessage(
35-
"patterns array must not contain null elements");
33+
assertPreconditionViolationFor(() -> ClassNameFilter.includeClassNamePatterns(new String[] { null }))//
34+
.withMessage("patterns array must not contain null elements");
3635
}
3736

3837
@Test
@@ -91,9 +90,8 @@ void excludeClassNamePatternsChecksPreconditions() {
9190
() -> ClassNameFilter.excludeClassNamePatterns((String[]) null));
9291
assertPreconditionViolationNotNullOrEmptyFor("patterns array",
9392
() -> ClassNameFilter.excludeClassNamePatterns(new String[0]));
94-
assertPreconditionViolationFor(
95-
() -> ClassNameFilter.excludeClassNamePatterns(new String[] { null })).withMessage(
96-
"patterns array must not contain null elements");
93+
assertPreconditionViolationFor(() -> ClassNameFilter.excludeClassNamePatterns(new String[] { null }))//
94+
.withMessage("patterns array must not contain null elements");
9795
}
9896

9997
@Test

0 commit comments

Comments
 (0)