Skip to content

Commit 0ec8d11

Browse files
committed
Introduce PreconditionAssertions as global test fixture
1 parent 2537103 commit 0ec8d11

File tree

20 files changed

+207
-237
lines changed

20 files changed

+207
-237
lines changed

junit-platform-commons/junit-platform-commons.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ dependencies {
1717
compileOnly(kotlin("stdlib"))
1818
compileOnly(kotlin("reflect"))
1919
compileOnly(libs.kotlinx.coroutines)
20+
21+
testFixturesImplementation(libs.assertj)
2022
}
2123

2224
tasks.compileJava {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2015-2025 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.platform.commons.test;
12+
13+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
14+
15+
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
16+
import org.assertj.core.api.ThrowableAssertAlternative;
17+
import org.junit.platform.commons.PreconditionViolationException;
18+
import org.junit.platform.commons.util.Preconditions;
19+
20+
/**
21+
* Collection of assertions for working with {@link Preconditions}.
22+
*
23+
* @since 6.0
24+
*/
25+
public final class PreconditionAssertions {
26+
27+
private PreconditionAssertions() {
28+
/* no-op */
29+
}
30+
31+
public static void assertPreconditionViolationNotNullFor(String name, ThrowingCallable throwingCallable) {
32+
assertPreconditionViolationFor(throwingCallable).withMessage("%s must not be null", name);
33+
}
34+
35+
public static void assertPreconditionViolationNotNullOrBlankFor(String name, ThrowingCallable throwingCallable) {
36+
assertPreconditionViolationFor(throwingCallable).withMessage("%s must not be null or blank", name);
37+
}
38+
39+
public static ThrowableAssertAlternative<PreconditionViolationException> assertPreconditionViolationFor(
40+
ThrowingCallable throwingCallable) {
41+
42+
return assertThatExceptionOfType(PreconditionViolationException.class).isThrownBy(throwingCallable);
43+
}
44+
45+
}

jupiter-tests/src/templates/resources/test/org/junit/jupiter/api/condition/DisabledOnJreConditionTests.java.jte

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
${licenseHeader}
88
package org.junit.jupiter.api.condition;
99

10-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1110
@for(var jre : supportedJresSortedByStringValue)<%--
1211
--%>import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava${jre.getVersion()};
1312
@endfor<%--
1413
--%>import static org.junit.jupiter.api.condition.JavaVersionPredicates.onKnownVersion;
14+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1515

1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.extension.ExecutionCondition;
18-
import org.junit.platform.commons.PreconditionViolationException;
1918

2019
/**
2120
* Unit tests for {@link DisabledOnJreCondition}, generated from
@@ -55,8 +54,7 @@ class DisabledOnJreConditionTests extends AbstractExecutionConditionTests {
5554
*/
5655
@Test
5756
void missingVersionDeclaration() {
58-
assertThatExceptionOfType(PreconditionViolationException.class)//
59-
.isThrownBy(this::evaluateCondition)//
57+
assertPreconditionViolationFor(this::evaluateCondition)//
6058
.withMessage("You must declare at least one JRE or version in @DisabledOnJre");
6159
}
6260

@@ -65,8 +63,7 @@ class DisabledOnJreConditionTests extends AbstractExecutionConditionTests {
6563
*/
6664
@Test
6765
void jreUndefined() {
68-
assertThatExceptionOfType(PreconditionViolationException.class)//
69-
.isThrownBy(this::evaluateCondition)//
66+
assertPreconditionViolationFor(this::evaluateCondition)//
7067
.withMessage("JRE.UNDEFINED is not supported in @DisabledOnJre");
7168
}
7269

@@ -75,8 +72,7 @@ class DisabledOnJreConditionTests extends AbstractExecutionConditionTests {
7572
*/
7673
@Test
7774
void version7() {
78-
assertThatExceptionOfType(PreconditionViolationException.class)//
79-
.isThrownBy(this::evaluateCondition)//
75+
assertPreconditionViolationFor(this::evaluateCondition)//
8076
.withMessage("Version [7] in @DisabledOnJre must be greater than or equal to 8");
8177
}
8278

jupiter-tests/src/templates/resources/test/org/junit/jupiter/api/condition/EnabledOnJreConditionTests.java.jte

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@
77
${licenseHeader}
88
package org.junit.jupiter.api.condition;
99

10-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1110
@for(var jre : supportedJresSortedByStringValue)<%--
1211
--%>import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava${jre.getVersion()};
1312
@endfor<%--
1413
--%>import static org.junit.jupiter.api.condition.JavaVersionPredicates.onKnownVersion;
14+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1515

1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.extension.ExecutionCondition;
18-
import org.junit.platform.commons.PreconditionViolationException;
1918

2019
/**
2120
* Unit tests for {@link EnabledOnJreCondition}, generated from
@@ -55,8 +54,7 @@ class EnabledOnJreConditionTests extends AbstractExecutionConditionTests {
5554
*/
5655
@Test
5756
void missingVersionDeclaration() {
58-
assertThatExceptionOfType(PreconditionViolationException.class)//
59-
.isThrownBy(this::evaluateCondition)//
57+
assertPreconditionViolationFor(this::evaluateCondition)//
6058
.withMessage("You must declare at least one JRE or version in @EnabledOnJre");
6159
}
6260

@@ -65,8 +63,7 @@ class EnabledOnJreConditionTests extends AbstractExecutionConditionTests {
6563
*/
6664
@Test
6765
void jreUndefined() {
68-
assertThatExceptionOfType(PreconditionViolationException.class)//
69-
.isThrownBy(this::evaluateCondition)//
66+
assertPreconditionViolationFor(this::evaluateCondition)//
7067
.withMessage("JRE.UNDEFINED is not supported in @EnabledOnJre");
7168
}
7269

@@ -75,8 +72,7 @@ class EnabledOnJreConditionTests extends AbstractExecutionConditionTests {
7572
*/
7673
@Test
7774
void version7() {
78-
assertThatExceptionOfType(PreconditionViolationException.class)//
79-
.isThrownBy(this::evaluateCondition)//
75+
assertPreconditionViolationFor(this::evaluateCondition)//
8076
.withMessage("Version [7] in @EnabledOnJre must be greater than or equal to 8");
8177
}
8278

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

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

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

13-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1413
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava17;
1514
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava18;
1615
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava19;
1716
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onKnownVersion;
17+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1818

1919
import org.junit.jupiter.api.Test;
2020
import org.junit.jupiter.api.extension.ExecutionCondition;
21-
import org.junit.platform.commons.PreconditionViolationException;
2221

2322
/**
2423
* Unit tests for {@link DisabledForJreRange}.
@@ -57,8 +56,7 @@ void enabledBecauseAnnotationIsNotPresent() {
5756
*/
5857
@Test
5958
void defaultValues() {
60-
assertThatExceptionOfType(PreconditionViolationException.class)//
61-
.isThrownBy(this::evaluateCondition)//
59+
assertPreconditionViolationFor(this::evaluateCondition)//
6260
.withMessage(
6361
"You must declare a non-default value for the minimum or maximum value in @DisabledForJreRange");
6462
}
@@ -116,8 +114,7 @@ void maxVersionMaxInteger() {
116114
*/
117115
@Test
118116
void minVersion7() {
119-
assertThatExceptionOfType(PreconditionViolationException.class)//
120-
.isThrownBy(this::evaluateCondition)//
117+
assertPreconditionViolationFor(this::evaluateCondition)//
121118
.withMessage("@DisabledForJreRange's minVersion [7] must be greater than or equal to 8");
122119
}
123120

@@ -126,8 +123,7 @@ void minVersion7() {
126123
*/
127124
@Test
128125
void maxVersion16() {
129-
assertThatExceptionOfType(PreconditionViolationException.class)//
130-
.isThrownBy(this::evaluateCondition)//
126+
assertPreconditionViolationFor(this::evaluateCondition)//
131127
.withMessage(
132128
"@DisabledForJreRange's minimum value [17] must be less than or equal to its maximum value [16]");
133129
}
@@ -137,8 +133,7 @@ void maxVersion16() {
137133
*/
138134
@Test
139135
void minAndMinVersion() {
140-
assertThatExceptionOfType(PreconditionViolationException.class)//
141-
.isThrownBy(this::evaluateCondition)//
136+
assertPreconditionViolationFor(this::evaluateCondition)//
142137
.withMessage(
143138
"@DisabledForJreRange's minimum value must be configured with either a JRE enum constant or numeric version, but not both");
144139
}
@@ -148,8 +143,7 @@ void minAndMinVersion() {
148143
*/
149144
@Test
150145
void maxAndMaxVersion() {
151-
assertThatExceptionOfType(PreconditionViolationException.class)//
152-
.isThrownBy(this::evaluateCondition)//
146+
assertPreconditionViolationFor(this::evaluateCondition)//
153147
.withMessage(
154148
"@DisabledForJreRange's maximum value must be configured with either a JRE enum constant or numeric version, but not both");
155149
}
@@ -159,8 +153,7 @@ void maxAndMaxVersion() {
159153
*/
160154
@Test
161155
void minGreaterThanMax() {
162-
assertThatExceptionOfType(PreconditionViolationException.class)//
163-
.isThrownBy(this::evaluateCondition)//
156+
assertPreconditionViolationFor(this::evaluateCondition)//
164157
.withMessage(
165158
"@DisabledForJreRange's minimum value [21] must be less than or equal to its maximum value [17]");
166159
}

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

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

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

13-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1413
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava17;
1514
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava18;
1615
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava19;
@@ -22,10 +21,10 @@
2221
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava25;
2322
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onJava26;
2423
import static org.junit.jupiter.api.condition.JavaVersionPredicates.onKnownVersion;
24+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
2525

2626
import org.junit.jupiter.api.Test;
2727
import org.junit.jupiter.api.extension.ExecutionCondition;
28-
import org.junit.platform.commons.PreconditionViolationException;
2928

3029
/**
3130
* Unit tests for {@link EnabledForJreRange @EnabledForJreRange}.
@@ -64,8 +63,7 @@ void enabledBecauseAnnotationIsNotPresent() {
6463
*/
6564
@Test
6665
void defaultValues() {
67-
assertThatExceptionOfType(PreconditionViolationException.class)//
68-
.isThrownBy(this::evaluateCondition)//
66+
assertPreconditionViolationFor(this::evaluateCondition)//
6967
.withMessage(
7068
"You must declare a non-default value for the minimum or maximum value in @EnabledForJreRange");
7169
}
@@ -123,8 +121,7 @@ void maxVersionMaxInteger() {
123121
*/
124122
@Test
125123
void minVersion7() {
126-
assertThatExceptionOfType(PreconditionViolationException.class)//
127-
.isThrownBy(this::evaluateCondition)//
124+
assertPreconditionViolationFor(this::evaluateCondition)//
128125
.withMessage("@EnabledForJreRange's minVersion [7] must be greater than or equal to 8");
129126
}
130127

@@ -133,8 +130,7 @@ void minVersion7() {
133130
*/
134131
@Test
135132
void maxVersion16() {
136-
assertThatExceptionOfType(PreconditionViolationException.class)//
137-
.isThrownBy(this::evaluateCondition)//
133+
assertPreconditionViolationFor(this::evaluateCondition)//
138134
.withMessage(
139135
"@EnabledForJreRange's minimum value [17] must be less than or equal to its maximum value [16]");
140136
}
@@ -144,8 +140,7 @@ void maxVersion16() {
144140
*/
145141
@Test
146142
void minAndMinVersion() {
147-
assertThatExceptionOfType(PreconditionViolationException.class)//
148-
.isThrownBy(this::evaluateCondition)//
143+
assertPreconditionViolationFor(this::evaluateCondition)//
149144
.withMessage(
150145
"@EnabledForJreRange's minimum value must be configured with either a JRE enum constant or numeric version, but not both");
151146
}
@@ -155,8 +150,7 @@ void minAndMinVersion() {
155150
*/
156151
@Test
157152
void maxAndMaxVersion() {
158-
assertThatExceptionOfType(PreconditionViolationException.class)//
159-
.isThrownBy(this::evaluateCondition)//
153+
assertPreconditionViolationFor(this::evaluateCondition)//
160154
.withMessage(
161155
"@EnabledForJreRange's maximum value must be configured with either a JRE enum constant or numeric version, but not both");
162156
}
@@ -166,8 +160,7 @@ void maxAndMaxVersion() {
166160
*/
167161
@Test
168162
void minGreaterThanMax() {
169-
assertThatExceptionOfType(PreconditionViolationException.class)//
170-
.isThrownBy(this::evaluateCondition)//
163+
assertPreconditionViolationFor(this::evaluateCondition)//
171164
.withMessage(
172165
"@EnabledForJreRange's minimum value [21] must be less than or equal to its maximum value [17]");
173166
}

jupiter-tests/src/test/java/org/junit/jupiter/api/extension/MediaTypeTests.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
package org.junit.jupiter.api.extension;
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.EqualsAndHashCodeAssertions.assertEqualsAndHashCode;
15+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
16+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationNotNullFor;
1617

1718
import java.nio.charset.StandardCharsets;
1819

1920
import org.junit.jupiter.api.Test;
20-
import org.junit.platform.commons.PreconditionViolationException;
2121

2222
class MediaTypeTests {
2323

@@ -41,15 +41,13 @@ void createWithCharset() {
4141

4242
@Test
4343
void parseWithInvalidMediaType() {
44-
var exception = assertThrows(PreconditionViolationException.class, () -> MediaType.parse("invalid"));
45-
assertEquals("Invalid media type: 'invalid'", exception.getMessage());
44+
assertPreconditionViolationFor(() -> MediaType.parse("invalid")).withMessage("Invalid media type: 'invalid'");
4645
}
4746

4847
@SuppressWarnings("DataFlowIssue")
4948
@Test
5049
void parseWithNullMediaType() {
51-
var exception = assertThrows(PreconditionViolationException.class, () -> MediaType.parse(null));
52-
assertEquals("value must not be null", exception.getMessage());
50+
assertPreconditionViolationNotNullFor("value", () -> MediaType.parse(null));
5351
}
5452

5553
@Test
@@ -60,4 +58,5 @@ void equals() {
6058

6159
assertEqualsAndHashCode(mediaType1, mediaType2, mediaType3);
6260
}
61+
6362
}

jupiter-tests/src/test/java/org/junit/jupiter/params/converter/TypedArgumentConverterTests.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.assertj.core.api.Assertions.assertThat;
1414
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
1515
import static org.junit.jupiter.api.Assertions.assertAll;
16+
import static org.junit.platform.commons.test.PreconditionAssertions.assertPreconditionViolationFor;
1617
import static org.mockito.Mockito.mock;
1718
import static org.mockito.Mockito.when;
1819

@@ -30,7 +31,6 @@
3031
import org.junit.jupiter.params.ParameterizedTest;
3132
import org.junit.jupiter.params.provider.NullSource;
3233
import org.junit.jupiter.params.provider.ValueSource;
33-
import org.junit.platform.commons.PreconditionViolationException;
3434
import org.junit.platform.commons.support.ReflectionSupport;
3535

3636
/**
@@ -51,12 +51,10 @@ class UnitTests {
5151
@SuppressWarnings("DataFlowIssue")
5252
@Test
5353
void preconditions() {
54-
assertThatExceptionOfType(PreconditionViolationException.class)//
55-
.isThrownBy(() -> new StringLengthArgumentConverter(null, Integer.class))//
54+
assertPreconditionViolationFor(() -> new StringLengthArgumentConverter(null, Integer.class))//
5655
.withMessage("sourceType must not be null");
5756

58-
assertThatExceptionOfType(PreconditionViolationException.class)//
59-
.isThrownBy(() -> new StringLengthArgumentConverter(String.class, null))//
57+
assertPreconditionViolationFor(() -> new StringLengthArgumentConverter(String.class, null))//
6058
.withMessage("targetType must not be null");
6159
}
6260

0 commit comments

Comments
 (0)