Skip to content

Commit b4aef17

Browse files
author
Vincent Potucek
committed
Add CleanupAssertions junit-team#5193 junit-team#5002
- https://docs.openrewrite.org/recipes/java/testing/junit/jupiterbestpractices Signed-off-by: Vincent Potucek <vpotucek@me.com>
1 parent 41af02f commit b4aef17

18 files changed

Lines changed: 169 additions & 173 deletions

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -508,8 +508,8 @@ void assertEqualsWithNullReferences() {
508508
Object null1 = null;
509509
Object null2 = null;
510510

511-
assertNull(null1);
512-
assertNull(null2);
511+
assertEquals(null1, null);
512+
assertEquals(null, null2);
513513
assertEquals(null1, null2);
514514
}
515515

@@ -529,7 +529,7 @@ void assertEqualsWithEquivalentStrings() {
529529
@Test
530530
void assertEqualsWithNullVsObject() {
531531
try {
532-
assertNull("foo");
532+
assertEquals(null, "foo");
533533
expectAssertionFailedError();
534534
}
535535
catch (AssertionFailedError ex) {
@@ -541,7 +541,7 @@ void assertEqualsWithNullVsObject() {
541541
@Test
542542
void assertEqualsWithObjectVsNull() {
543543
try {
544-
assertNull("foo");
544+
assertEquals("foo", null);
545545
expectAssertionFailedError();
546546
}
547547
catch (AssertionFailedError ex) {
@@ -553,7 +553,7 @@ void assertEqualsWithObjectVsNull() {
553553
@Test
554554
void assertEqualsWithObjectWithNullStringReturnedFromToStringVsNull() {
555555
try {
556-
assertNull("null");
556+
assertEquals("null", null);
557557
expectAssertionFailedError();
558558
}
559559
catch (AssertionFailedError ex) {
@@ -566,7 +566,7 @@ void assertEqualsWithObjectWithNullStringReturnedFromToStringVsNull() {
566566
@Test
567567
void assertEqualsWithNullVsObjectWithNullStringReturnedFromToString() {
568568
try {
569-
assertNull("null");
569+
assertEquals(null, "null");
570570
expectAssertionFailedError();
571571
}
572572
catch (AssertionFailedError ex) {
@@ -579,7 +579,7 @@ void assertEqualsWithNullVsObjectWithNullStringReturnedFromToString() {
579579
@Test
580580
void assertEqualsWithNullVsObjectAndMessageSupplier() {
581581
try {
582-
assertNull("foo", () -> "test");
582+
assertEquals(null, "foo", () -> "test");
583583
expectAssertionFailedError();
584584
}
585585
catch (AssertionFailedError ex) {
@@ -592,7 +592,7 @@ void assertEqualsWithNullVsObjectAndMessageSupplier() {
592592
@Test
593593
void assertEqualsWithObjectVsNullAndMessageSupplier() {
594594
try {
595-
assertNull("foo", () -> "test");
595+
assertEquals("foo", null, () -> "test");
596596
expectAssertionFailedError();
597597
}
598598
catch (AssertionFailedError ex) {
@@ -611,7 +611,7 @@ void assertEqualsInvokesEqualsMethodForIdenticalObjects() {
611611
@Test
612612
void assertEqualsWithUnequalObjectWhoseToStringImplementationThrowsAnException() {
613613
try {
614-
assertEquals("foo", new ToStringThrowsException());
614+
assertEquals(new ToStringThrowsException(), "foo");
615615
}
616616
catch (AssertionFailedError ex) {
617617
assertMessageStartsWith(ex, "expected: <" + ToStringThrowsException.class.getName() + "@");

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ void assertIterableEqualsIterableVsNull() {
132132
}
133133

134134
try {
135-
assertIterableEquals(null, listOf('a', 1, new Object(), 10L));
135+
assertIterableEquals(listOf('a', 1, new Object(), 10L), null);
136136
expectAssertionFailedError();
137137
}
138138
catch (AssertionFailedError ex) {
@@ -173,7 +173,7 @@ void assertIterableEqualsIterableVsNullAndMessage() {
173173
}
174174

175175
try {
176-
assertIterableEquals(null, listOf("hello", 42), "message");
176+
assertIterableEquals(listOf("hello", 42), null, "message");
177177
expectAssertionFailedError();
178178
}
179179
catch (AssertionFailedError ex) {
@@ -217,7 +217,7 @@ void assertIterableEqualsIterableVsNullAndMessageSupplier() {
217217
}
218218

219219
try {
220-
assertIterableEquals(null, listOf(listOf("a"), listOf()), () -> "message");
220+
assertIterableEquals(listOf(listOf("a"), listOf()), null, () -> "message");
221221
expectAssertionFailedError();
222222
}
223223
catch (AssertionFailedError ex) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ void assertNotSameWithDifferentObjectsAndMessageSupplier() {
3939

4040
@Test
4141
void assertNotSameWithObjectVsNull() {
42-
assertNotSame(null, new Object());
42+
assertNotSame(new Object(), null);
4343
}
4444

4545
@Test

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void assertSameWithSameObject() {
4444
void assertSameWithObjectVsNull() {
4545
Object expected = new Object();
4646
try {
47-
assertSame(null, expected);
47+
assertSame(expected, null);
4848
expectAssertionFailedError();
4949
}
5050
catch (AssertionFailedError ex) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ void executableModeMustNotBeNull() {
257257
}
258258

259259
private void assert1Equals48Directly() {
260-
assertEquals(1, 48);
260+
Assertions.assertEquals(1, 48);
261261
}
262262

263263
private void assert1Equals49ReflectivelyAndUnwrapInvocationTargetException() throws Throwable {

jupiter-tests/src/test/java/org/junit/jupiter/engine/ExceptionHandlingTests.java

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

1111
package org.junit.jupiter.engine;
1212

13-
import static org.junit.jupiter.api.Assertions.fail;
1413
import static org.junit.jupiter.api.Assumptions.assumeFalse;
1514
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS;
1615
import static org.junit.jupiter.api.TestInstance.Lifecycle.PER_METHOD;
@@ -31,6 +30,7 @@
3130

3231
import org.junit.jupiter.api.AfterAll;
3332
import org.junit.jupiter.api.AfterEach;
33+
import org.junit.jupiter.api.Assertions;
3434
import org.junit.jupiter.api.BeforeAll;
3535
import org.junit.jupiter.api.BeforeEach;
3636
import org.junit.jupiter.api.Test;
@@ -290,7 +290,7 @@ void succeedingTest() {
290290

291291
@Test
292292
void failingTest() {
293-
fail("always fails");
293+
Assertions.fail("always fails");
294294
}
295295

296296
@Test

jupiter-tests/src/test/java/org/junit/jupiter/engine/NestedTestClassesTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import static org.assertj.core.api.Assertions.assertThat;
1414
import static org.junit.jupiter.api.Assertions.assertAll;
1515
import static org.junit.jupiter.api.Assertions.assertEquals;
16-
import static org.junit.jupiter.api.Assertions.fail;
1716
import static org.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
1817
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
1918
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectMethod;
@@ -32,6 +31,7 @@
3231
import java.util.regex.Pattern;
3332

3433
import org.junit.jupiter.api.AfterEach;
34+
import org.junit.jupiter.api.Assertions;
3535
import org.junit.jupiter.api.BeforeEach;
3636
import org.junit.jupiter.api.Named;
3737
import org.junit.jupiter.api.Nested;
@@ -326,7 +326,7 @@ void successful() {
326326

327327
@Test
328328
void failing() {
329-
fail("Something went horribly wrong");
329+
Assertions.fail("Something went horribly wrong");
330330
}
331331
}
332332
}
@@ -375,7 +375,7 @@ void successful() {
375375

376376
@Test
377377
void failing() {
378-
fail("Something went horribly wrong");
378+
Assertions.fail("Something went horribly wrong");
379379
}
380380

381381
@Nested
@@ -397,7 +397,7 @@ void successful() {
397397

398398
@Test
399399
void failing() {
400-
fail("Something went horribly wrong");
400+
Assertions.fail("Something went horribly wrong");
401401
}
402402
}
403403
}
@@ -411,7 +411,7 @@ class NestedInInterface {
411411

412412
@Test
413413
void notExecutedByImplementingClass() {
414-
fail("class in interface is static and should have been filtered out");
414+
Assertions.fail("class in interface is static and should have been filtered out");
415415
}
416416
}
417417

@@ -428,7 +428,7 @@ void successful() {
428428

429429
@Test
430430
void failing() {
431-
fail("something went wrong");
431+
Assertions.fail("something went wrong");
432432
}
433433

434434
@Nested

jupiter-tests/src/test/java/org/junit/jupiter/engine/TestClassInheritanceTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,21 +116,21 @@ void beforeAndAfterMethodsInTestClassHierarchy() {
116116
// @formatter:on
117117

118118
// @formatter:off
119-
assertEquals(callSequence, asList(
120-
"beforeAll1",
119+
assertEquals(asList(
120+
"beforeAll1",
121121
"beforeAll2",
122-
"beforeAll3",
123-
"beforeEach1",
124-
"beforeEach2",
125-
"beforeEach3",
126-
"test3",
127-
"afterEach3",
128-
"afterEach2",
129-
"afterEach1",
130-
"afterAll3",
122+
"beforeAll3",
123+
"beforeEach1",
124+
"beforeEach2",
125+
"beforeEach3",
126+
"test3",
127+
"afterEach3",
128+
"afterEach2",
129+
"afterEach1",
130+
"afterAll3",
131131
"afterAll2",
132-
"afterAll1"
133-
), "wrong call sequence");
132+
"afterAll1"
133+
), callSequence, "wrong call sequence");
134134
// @formatter:on
135135
}
136136

jupiter-tests/src/test/java/org/junit/jupiter/engine/TestInstanceLifecycleConfigurationTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private void performAssertions(Class<?> testClass, Map<String, String> configPar
138138
executionResults.testEvents().assertStatistics(//
139139
stats -> stats.started(numTests).finished(numTests));
140140

141-
assertEquals(methodsInvoked, Arrays.asList(methods));
141+
assertEquals(Arrays.asList(methods), methodsInvoked);
142142
}
143143

144144
// -------------------------------------------------------------------------

jupiter-tests/src/test/java/org/junit/jupiter/engine/bridge/AbstractNonGenericTests.java

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

1111
package org.junit.jupiter.engine.bridge;
1212

13-
import static org.junit.jupiter.api.Assertions.assertEquals;
14-
13+
import org.junit.jupiter.api.Assertions;
1514
import org.junit.jupiter.api.Test;
1615
import org.junit.jupiter.api.extension.ExtendWith;
1716

@@ -29,7 +28,7 @@ void mA() {
2928
@Test
3029
void test(Number value) {
3130
BridgeMethodTests.sequence.add("A.test(Number)");
32-
assertEquals(42, value);
31+
Assertions.assertEquals(42, value);
3332
}
3433

3534
static class B extends AbstractNonGenericTests {
@@ -42,7 +41,7 @@ void mB() {
4241
@Test
4342
void test(Byte value) {
4443
BridgeMethodTests.sequence.add("B.test(Byte)");
45-
assertEquals(123, value.intValue());
44+
Assertions.assertEquals(123, value.intValue());
4645
}
4746

4847
}
@@ -58,7 +57,7 @@ void mC() {
5857
@Test
5958
void test(Byte value) {
6059
BridgeMethodTests.sequence.add("C.test(Byte)");
61-
assertEquals(123, value.intValue());
60+
Assertions.assertEquals(123, value.intValue());
6261
}
6362

6463
}

0 commit comments

Comments
 (0)