Skip to content

Commit 1312cda

Browse files
neronsodasbrannen
authored andcommitted
Use PreconditionAssertions wherever feasible
Closes #4943 Closes #5019
1 parent 805adad commit 1312cda

File tree

76 files changed

+659
-957
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+659
-957
lines changed

junit-platform-commons/src/testFixtures/java/org/junit/platform/commons/test/PreconditionAssertions.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,22 @@ public static void assertPreconditionViolationNotNullFor(String name, ThrowingCa
3232
assertPreconditionViolationFor(throwingCallable).withMessage("%s must not be null", name);
3333
}
3434

35+
public static void assertPreconditionViolationNotBlankFor(String name, ThrowingCallable throwingCallable) {
36+
assertPreconditionViolationFor(throwingCallable).withMessageContaining("%s must not be blank", name);
37+
}
38+
39+
public static void assertPreconditionViolationNotEmptyFor(String name, ThrowingCallable throwingCallable) {
40+
assertPreconditionViolationFor(throwingCallable).withMessage("%s must not be empty", name);
41+
}
42+
3543
public static void assertPreconditionViolationNotNullOrBlankFor(String name, ThrowingCallable throwingCallable) {
3644
assertPreconditionViolationFor(throwingCallable).withMessage("%s must not be null or blank", name);
3745
}
3846

47+
public static void assertPreconditionViolationNotNullOrEmptyFor(String name, ThrowingCallable throwingCallable) {
48+
assertPreconditionViolationFor(throwingCallable).withMessage("%s must not be null or empty", name);
49+
}
50+
3951
public static ThrowableAssertAlternative<PreconditionViolationException> assertPreconditionViolationFor(
4052
ThrowingCallable throwingCallable) {
4153

junit-vintage-engine/src/test/java/org/junit/vintage/engine/descriptor/OrFilterTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
package org.junit.vintage.engine.descriptor;
1212

1313
import static org.junit.jupiter.api.Assertions.assertEquals;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
1514
import static org.junit.jupiter.api.Assertions.assertTrue;
15+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotEmptyFor;
1616
import static org.mockito.ArgumentMatchers.any;
1717
import static org.mockito.ArgumentMatchers.same;
1818
import static org.mockito.Mockito.mock;
@@ -23,7 +23,6 @@
2323
import java.util.Set;
2424

2525
import org.junit.jupiter.api.Test;
26-
import org.junit.platform.commons.PreconditionViolationException;
2726
import org.junit.runner.Description;
2827
import org.junit.runner.manipulation.Filter;
2928

@@ -34,8 +33,7 @@ class OrFilterTests {
3433

3534
@Test
3635
void exceptionWithoutAnyFilters() {
37-
var actual = assertThrows(PreconditionViolationException.class, () -> new OrFilter(Set.of()));
38-
assertEquals("filters must not be empty", actual.getMessage());
36+
assertPreconditionViolationNotEmptyFor("filters", () -> new OrFilter(Set.of()));
3937
}
4038

4139
@Test

jupiter-tests/src/test/java/org/junit/jupiter/api/AssertAllAssertionsTests.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212

1313
import static java.util.Arrays.asList;
1414
import static org.assertj.core.api.Assertions.assertThat;
15-
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
1615
import static org.junit.jupiter.api.Assertions.assertAll;
1716
import static org.junit.jupiter.api.Assertions.assertEquals;
1817
import static org.junit.jupiter.api.Assertions.assertFalse;
1918
import static org.junit.jupiter.api.Assertions.assertNotNull;
2019
import static org.junit.jupiter.api.Assertions.assertThrows;
2120
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullFor;
22+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullOrEmptyFor;
2223

2324
import java.io.IOException;
2425
import java.util.Collection;
2526
import java.util.List;
2627
import java.util.stream.Stream;
2728

2829
import org.junit.jupiter.api.function.Executable;
29-
import org.junit.platform.commons.PreconditionViolationException;
3030
import org.opentest4j.AssertionFailedError;
3131
import org.opentest4j.MultipleFailuresError;
3232

@@ -40,36 +40,36 @@ class AssertAllAssertionsTests {
4040
@SuppressWarnings("DataFlowIssue")
4141
@Test
4242
void assertAllWithNullExecutableArray() {
43-
assertPrecondition("executables array must not be null or empty", () -> assertAll((Executable[]) null));
43+
assertPreconditionViolationNotNullOrEmptyFor("executables array", () -> assertAll((Executable[]) null));
4444
}
4545

4646
@SuppressWarnings("DataFlowIssue")
4747
@Test
4848
void assertAllWithNullExecutableCollection() {
49-
assertPrecondition("executables collection must not be null", () -> assertAll((Collection<Executable>) null));
49+
assertPreconditionViolationNotNullFor("executables collection", () -> assertAll((Collection<Executable>) null));
5050
}
5151

5252
@SuppressWarnings("DataFlowIssue")
5353
@Test
5454
void assertAllWithNullExecutableStream() {
55-
assertPrecondition("executables stream must not be null", () -> assertAll((Stream<Executable>) null));
55+
assertPreconditionViolationNotNullFor("executables stream", () -> assertAll((Stream<Executable>) null));
5656
}
5757

5858
@SuppressWarnings("DataFlowIssue")
5959
@Test
6060
void assertAllWithNullInExecutableArray() {
61-
assertPrecondition("individual executables must not be null", () -> assertAll((Executable) null));
61+
assertPreconditionViolationNotNullFor("individual executables", () -> assertAll((Executable) null));
6262
}
6363

6464
@Test
6565
void assertAllWithNullInExecutableCollection() {
66-
assertPrecondition("individual executables must not be null", () -> assertAll(asList((Executable) null)));
66+
assertPreconditionViolationNotNullFor("individual executables", () -> assertAll(asList((Executable) null)));
6767
}
6868

6969
@SuppressWarnings("DataFlowIssue")
7070
@Test
7171
void assertAllWithNullInExecutableStream() {
72-
assertPrecondition("individual executables must not be null", () -> assertAll(Stream.of((Executable) null)));
72+
assertPreconditionViolationNotNullFor("individual executables", () -> assertAll(Stream.of((Executable) null)));
7373
}
7474

7575
@Test
@@ -198,11 +198,6 @@ void assertAllWithParallelStream() {
198198
assertThat(multipleFailuresError.getFailures()).hasSize(100).doesNotContainNull();
199199
}
200200

201-
private void assertPrecondition(String msg, Executable executable) {
202-
PreconditionViolationException exception = assertThrows(PreconditionViolationException.class, executable);
203-
assertMessageEquals(exception, msg);
204-
}
205-
206201
@SafeVarargs
207202
static void assertExpectedExceptionTypes(MultipleFailuresError multipleFailuresError,
208203
Class<? extends Throwable>... exceptionTypes) {

jupiter-tests/src/test/java/org/junit/jupiter/api/AssertLinesMatchAssertionsTests.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
import static org.junit.jupiter.api.AssertLinesMatch.isFastForwardLine;
1414
import static org.junit.jupiter.api.AssertLinesMatch.parseFastForwardLimit;
15-
import static org.junit.jupiter.api.AssertionTestUtils.assertMessageEquals;
1615
import static org.junit.jupiter.api.Assertions.assertAll;
1716
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
1817
import static org.junit.jupiter.api.Assertions.assertEquals;
1918
import static org.junit.jupiter.api.Assertions.assertFalse;
2019
import static org.junit.jupiter.api.Assertions.assertLinesMatch;
2120
import static org.junit.jupiter.api.Assertions.assertThrows;
2221
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
23+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullFor;
2324

2425
import java.util.ArrayList;
2526
import java.util.Arrays;
@@ -31,7 +32,6 @@
3132
import java.util.stream.Stream;
3233

3334
import org.jspecify.annotations.Nullable;
34-
import org.junit.platform.commons.PreconditionViolationException;
3535
import org.opentest4j.AssertionFailedError;
3636

3737
/**
@@ -97,20 +97,18 @@ void assertLinesMatchUsingFastForwardMarkerWithLimit3() {
9797
@Test
9898
@SuppressWarnings({ "unchecked", "rawtypes", "DataFlowIssue" })
9999
void assertLinesMatchWithNullFails() {
100-
assertThrows(PreconditionViolationException.class, () -> assertLinesMatch(null, (List) null));
101-
assertThrows(PreconditionViolationException.class, () -> assertLinesMatch(null, Collections.emptyList()));
102-
assertThrows(PreconditionViolationException.class, () -> assertLinesMatch(Collections.emptyList(), null));
100+
assertPreconditionViolationFor(() -> assertLinesMatch(null, (List) null));
101+
assertPreconditionViolationFor(() -> assertLinesMatch(null, Collections.emptyList()));
102+
assertPreconditionViolationFor(() -> assertLinesMatch(Collections.emptyList(), null));
103103
}
104104

105105
@Test
106106
void assertLinesMatchWithNullElementsFails() {
107107
var list = List.of("1", "2", "3");
108108
var withNullElement = Arrays.asList("1", null, "3"); // List.of() doesn't permit null values.
109109
assertDoesNotThrow(() -> assertLinesMatch(withNullElement, withNullElement));
110-
var e1 = assertThrows(PreconditionViolationException.class, () -> assertLinesMatch(withNullElement, list));
111-
assertEquals("expected line must not be null", e1.getMessage());
112-
var e2 = assertThrows(PreconditionViolationException.class, () -> assertLinesMatch(list, withNullElement));
113-
assertEquals("actual line must not be null", e2.getMessage());
110+
assertPreconditionViolationNotNullFor("expected line", () -> assertLinesMatch(withNullElement, list));
111+
assertPreconditionViolationNotNullFor("actual line", () -> assertLinesMatch(list, withNullElement));
114112
}
115113

116114
private void assertError(AssertionFailedError error, String expectedMessage, List<String> expectedLines,
@@ -215,12 +213,12 @@ void assertLinesMatchParseFastForwardLimit() {
215213
() -> assertEquals(9, parseFastForwardLimit(">> 9 >>")),
216214
() -> assertEquals(9, parseFastForwardLimit(" >> 9 >> ")),
217215
() -> assertEquals(9, parseFastForwardLimit(" >> 9 >> ")));
218-
Throwable error = assertThrows(PreconditionViolationException.class, () -> parseFastForwardLimit(">>0>>"));
219-
assertMessageEquals(error, "fast-forward(0) limit must be greater than zero");
220-
error = assertThrows(PreconditionViolationException.class, () -> parseFastForwardLimit(">>-1>>"));
221-
assertMessageEquals(error, "fast-forward(-1) limit must be greater than zero");
222-
error = assertThrows(PreconditionViolationException.class, () -> parseFastForwardLimit(">>-2147483648>>"));
223-
assertMessageEquals(error, "fast-forward(-2147483648) limit must be greater than zero");
216+
assertPreconditionViolationFor(() -> parseFastForwardLimit(">>0>>"))//
217+
.withMessage("fast-forward(0) limit must be greater than zero");
218+
assertPreconditionViolationFor(() -> parseFastForwardLimit(">>-1>>"))//
219+
.withMessage("fast-forward(-1) limit must be greater than zero");
220+
assertPreconditionViolationFor(() -> parseFastForwardLimit(">>-2147483648>>"))//
221+
.withMessage("fast-forward(-2147483648) limit must be greater than zero");
224222
}
225223

226224
@Test

jupiter-tests/src/test/java/org/junit/jupiter/api/DynamicTestTests.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import static org.junit.jupiter.api.Assertions.assertThrows;
1616
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
1717
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
18+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1819

1920
import java.lang.reflect.InvocationTargetException;
2021
import java.lang.reflect.Method;
@@ -29,7 +30,6 @@
2930
import org.jspecify.annotations.Nullable;
3031
import org.junit.jupiter.api.function.Executable;
3132
import org.junit.jupiter.api.function.ThrowingConsumer;
32-
import org.junit.platform.commons.PreconditionViolationException;
3333
import org.junit.platform.commons.support.ReflectionSupport;
3434
import org.opentest4j.AssertionFailedError;
3535

@@ -50,12 +50,9 @@ void streamFromStreamPreconditions() {
5050
};
5151
Function<Object, String> displayNameGenerator = Object::toString;
5252

53-
assertThrows(PreconditionViolationException.class,
54-
() -> DynamicTest.stream((Stream<?>) null, displayNameGenerator, testExecutor));
55-
assertThrows(PreconditionViolationException.class,
56-
() -> DynamicTest.stream(Stream.empty(), null, testExecutor));
57-
assertThrows(PreconditionViolationException.class,
58-
() -> DynamicTest.stream(Stream.empty(), displayNameGenerator, null));
53+
assertPreconditionViolationFor(() -> DynamicTest.stream((Stream<?>) null, displayNameGenerator, testExecutor));
54+
assertPreconditionViolationFor(() -> DynamicTest.stream(Stream.empty(), null, testExecutor));
55+
assertPreconditionViolationFor(() -> DynamicTest.stream(Stream.empty(), displayNameGenerator, null));
5956
}
6057

6158
@SuppressWarnings("DataFlowIssue")
@@ -65,12 +62,10 @@ void streamFromIteratorPreconditions() {
6562
};
6663
Function<Object, String> displayNameGenerator = Object::toString;
6764

68-
assertThrows(PreconditionViolationException.class,
65+
assertPreconditionViolationFor(
6966
() -> DynamicTest.stream((Iterator<?>) null, displayNameGenerator, testExecutor));
70-
assertThrows(PreconditionViolationException.class,
71-
() -> DynamicTest.stream(emptyIterator(), null, testExecutor));
72-
assertThrows(PreconditionViolationException.class,
73-
() -> DynamicTest.stream(emptyIterator(), displayNameGenerator, null));
67+
assertPreconditionViolationFor(() -> DynamicTest.stream(emptyIterator(), null, testExecutor));
68+
assertPreconditionViolationFor(() -> DynamicTest.stream(emptyIterator(), displayNameGenerator, null));
7469
}
7570

7671
@SuppressWarnings("DataFlowIssue")
@@ -79,9 +74,8 @@ void streamFromStreamWithNamesPreconditions() {
7974
ThrowingConsumer<Object> testExecutor = input -> {
8075
};
8176

82-
assertThrows(PreconditionViolationException.class,
83-
() -> DynamicTest.stream((Stream<? extends Named<Object>>) null, testExecutor));
84-
assertThrows(PreconditionViolationException.class, () -> DynamicTest.stream(Stream.empty(), null));
77+
assertPreconditionViolationFor(() -> DynamicTest.stream((Stream<? extends Named<Object>>) null, testExecutor));
78+
assertPreconditionViolationFor(() -> DynamicTest.stream(Stream.empty(), null));
8579
}
8680

8781
@SuppressWarnings("DataFlowIssue")
@@ -90,23 +84,21 @@ void streamFromIteratorWithNamesPreconditions() {
9084
ThrowingConsumer<Object> testExecutor = input -> {
9185
};
9286

93-
assertThrows(PreconditionViolationException.class,
87+
assertPreconditionViolationFor(
9488
() -> DynamicTest.stream((Iterator<? extends Named<Object>>) null, testExecutor));
95-
assertThrows(PreconditionViolationException.class, () -> DynamicTest.stream(emptyIterator(), null));
89+
assertPreconditionViolationFor(() -> DynamicTest.stream(emptyIterator(), null));
9690
}
9791

9892
@SuppressWarnings("DataFlowIssue")
9993
@Test
10094
void streamFromStreamWithNamedExecutablesPreconditions() {
101-
assertThrows(PreconditionViolationException.class,
102-
() -> DynamicTest.stream((Stream<DummyNamedExecutableForTests>) null));
95+
assertPreconditionViolationFor(() -> DynamicTest.stream((Stream<DummyNamedExecutableForTests>) null));
10396
}
10497

10598
@SuppressWarnings("DataFlowIssue")
10699
@Test
107100
void streamFromIteratorWithNamedExecutablesPreconditions() {
108-
assertThrows(PreconditionViolationException.class,
109-
() -> DynamicTest.stream((Iterator<DummyNamedExecutableForTests>) null));
101+
assertPreconditionViolationFor(() -> DynamicTest.stream((Iterator<DummyNamedExecutableForTests>) null));
110102
}
111103

112104
@Test

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/DisabledIfEnvironmentVariableConditionTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,14 @@
1010

1111
package org.junit.jupiter.api.condition;
1212

13-
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
1513
import static org.junit.jupiter.api.condition.DisabledIfEnvironmentVariableIntegrationTests.ENIGMA;
1614
import static org.junit.jupiter.api.condition.DisabledIfEnvironmentVariableIntegrationTests.KEY1;
1715
import static org.junit.jupiter.api.condition.DisabledIfEnvironmentVariableIntegrationTests.KEY2;
16+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotBlankFor;
1817

1918
import org.jspecify.annotations.Nullable;
2019
import org.junit.jupiter.api.Test;
2120
import org.junit.jupiter.api.extension.ExecutionCondition;
22-
import org.junit.platform.commons.PreconditionViolationException;
2321

2422
/**
2523
* Unit tests for {@link DisabledIfEnvironmentVariableCondition}.
@@ -68,17 +66,15 @@ void enabledBecauseAnnotationIsNotPresent() {
6866
*/
6967
@Test
7068
void blankNamedAttribute() {
71-
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
72-
assertThat(exception).hasMessageContaining("The 'named' attribute must not be blank");
69+
assertPreconditionViolationNotBlankFor("The 'named' attribute", this::evaluateCondition);
7370
}
7471

7572
/**
7673
* @see DisabledIfEnvironmentVariableIntegrationTests#blankMatchesAttribute()
7774
*/
7875
@Test
7976
void blankMatchesAttribute() {
80-
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
81-
assertThat(exception).hasMessageContaining("The 'matches' attribute must not be blank");
77+
assertPreconditionViolationNotBlankFor("The 'matches' attribute", this::evaluateCondition);
8278
}
8379

8480
/**

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/DisabledIfSystemPropertyConditionTests.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010

1111
package org.junit.jupiter.api.condition;
1212

13-
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
13+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotBlankFor;
1514

1615
import org.junit.jupiter.api.AfterAll;
1716
import org.junit.jupiter.api.BeforeAll;
1817
import org.junit.jupiter.api.Test;
1918
import org.junit.jupiter.api.extension.ExecutionCondition;
20-
import org.junit.platform.commons.PreconditionViolationException;
2119

2220
/**
2321
* Unit tests for {@link DisabledIfSystemPropertyCondition}.
@@ -64,17 +62,15 @@ void enabledBecauseAnnotationIsNotPresent() {
6462
*/
6563
@Test
6664
void blankNamedAttribute() {
67-
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
68-
assertThat(exception).hasMessageContaining("The 'named' attribute must not be blank");
65+
assertPreconditionViolationNotBlankFor("The 'named' attribute", this::evaluateCondition);
6966
}
7067

7168
/**
7269
* @see DisabledIfSystemPropertyIntegrationTests#blankMatchesAttribute()
7370
*/
7471
@Test
7572
void blankMatchesAttribute() {
76-
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
77-
assertThat(exception).hasMessageContaining("The 'matches' attribute must not be blank");
73+
assertPreconditionViolationNotBlankFor("The 'matches' attribute", this::evaluateCondition);
7874
}
7975

8076
/**

jupiter-tests/src/test/java/org/junit/jupiter/api/condition/DisabledOnOsConditionTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
package org.junit.jupiter.api.condition;
1212

13-
import static org.assertj.core.api.Assertions.assertThat;
14-
import static org.junit.jupiter.api.Assertions.assertThrows;
1513
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onAix;
1614
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onArchitecture;
1715
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onFreebsd;
@@ -20,10 +18,10 @@
2018
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onOpenbsd;
2119
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onSolaris;
2220
import static org.junit.jupiter.api.condition.EnabledOnOsIntegrationTests.onWindows;
21+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
2322

2423
import org.junit.jupiter.api.Test;
2524
import org.junit.jupiter.api.extension.ExecutionCondition;
26-
import org.junit.platform.commons.PreconditionViolationException;
2725

2826
/**
2927
* Unit tests for {@link DisabledOnOsCondition}.
@@ -63,8 +61,8 @@ void enabledBecauseAnnotationIsNotPresent() {
6361
*/
6462
@Test
6563
void missingOsAndArchitectureDeclaration() {
66-
Exception exception = assertThrows(PreconditionViolationException.class, this::evaluateCondition);
67-
assertThat(exception).hasMessageContaining("You must declare at least one OS or architecture");
64+
assertPreconditionViolationFor(this::evaluateCondition)//
65+
.withMessageContaining("You must declare at least one OS or architecture");
6866
}
6967

7068
/**

0 commit comments

Comments
 (0)