From 5a1217cafed02d1af941d5fba85f4e69e68f0d6e Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Fri, 24 May 2019 18:24:24 +0200 Subject: [PATCH] Migrate JUnit Jupiter assertions to AssertJ Migrate all existing JUnit Jupiter based assertions to AssertJ and add Checkstyle rules to ensure they don't return. See gh-23022 --- .../jupiter/ComposedSpringExtensionTests.java | 13 +- .../jupiter/DisabledIfConditionTests.java | 42 +++--- .../junit/jupiter/DisabledIfTests.java | 7 +- .../context/junit/jupiter/EnabledIfTests.java | 9 +- ...reAndAfterMethodsSpringExtensionTests.java | 36 ++--- ...RegisterExtensionSpringExtensionTests.java | 127 ++++++++---------- .../SpringExtensionParameterizedTests.java | 9 +- .../junit/jupiter/SpringExtensionTests.java | 92 ++++++------- ...terAutowiredConstructorInjectionTests.java | 19 ++- ...JUnitJupiterConstructorInjectionTests.java | 21 ++- .../SpringManagedJupiterExtensionTests.java | 4 +- ...ConstructorAnnotationIntegrationTests.java | 19 ++- ...haractersInterfaceDefaultMethodsTests.java | 9 +- .../generics/GenericComicCharactersTests.java | 13 +- ...jectionWithSpringAndJUnitJupiterTests.java | 22 +-- ...edTestsWithSpringAndJUnitJupiterTests.java | 8 +- ...stsWithSqlScriptsAndJUnitJupiterTests.java | 10 +- ...ParallelExecutionSpringExtensionTests.java | 6 +- .../context/junit4/JUnitTestingUtils.java | 45 +++---- .../match/MockRestRequestMatchersTests.java | 68 +++++----- src/checkstyle/checkstyle.xml | 16 ++- 21 files changed, 277 insertions(+), 318 deletions(-) diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/ComposedSpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/ComposedSpringExtensionTests.java index 2e9ed96c77a..d10617fc431 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/ComposedSpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/ComposedSpringExtensionTests.java @@ -28,8 +28,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate the composability of annotations from @@ -61,16 +60,16 @@ class ComposedSpringExtensionTests { @Test @DisplayName("ApplicationContext injected into method") void applicationContextInjected(ApplicationContext applicationContext) { - assertNotNull(applicationContext, "ApplicationContext should have been injected into method by Spring"); - assertEquals(dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("ApplicationContext should have been injected into method by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(dilbert); } @Test @DisplayName("Spring @Beans injected into fields") void springBeansInjected() { - assertNotNull(dilbert, "Person should have been @Autowired by Spring"); - assertEquals("Dilbert", dilbert.getName(), "Person's name"); - assertEquals(2, people.size(), "Number of Person objects in context"); + assertThat(dilbert).as("Person should have been @Autowired by Spring").isNotNull(); + assertThat(dilbert.getName()).as("Person's name").isEqualTo("Dilbert"); + assertThat(people).as("Number of Person objects in context").hasSize(2); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfConditionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfConditionTests.java index c9b5e41ce02..506df209461 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfConditionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfConditionTests.java @@ -30,11 +30,10 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; import org.springframework.util.ReflectionUtils; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; -import static org.mockito.Mockito.mock; +import static org.mockito.BDDMockito.mock; /** * Tests for {@link DisabledIfCondition} that verify actual condition evaluation @@ -62,34 +61,30 @@ class DisabledIfConditionTests { @Test void disabledByEmptyExpression() { - // @formatter:off - assertAll( - () -> assertExpressionIsBlank("emptyExpression"), - () -> assertExpressionIsBlank("blankExpression") - ); - // @formatter:on + assertExpressionIsBlank("emptyExpression"); + assertExpressionIsBlank("blankExpression"); } @Test void invalidExpressionEvaluationType() { String methodName = "nonBooleanOrStringExpression"; - IllegalStateException exception = assertThrows(IllegalStateException.class, - () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); - Method method = ReflectionUtils.findMethod(getClass(), methodName); - assertThat(exception).hasMessage("@DisabledIf(\"#{6 * 7}\") on " + method + " must evaluate to a String or a Boolean, not java.lang.Integer"); + assertThatIllegalStateException() + .isThrownBy(() -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))) + .withMessageContaining( + "@DisabledIf(\"#{6 * 7}\") on " + method + " must evaluate to a String or a Boolean, not java.lang.Integer"); } @Test void unsupportedStringEvaluationValue() { String methodName = "stringExpressionThatIsNeitherTrueNorFalse"; - IllegalStateException exception = assertThrows(IllegalStateException.class, - () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); - Method method = ReflectionUtils.findMethod(getClass(), methodName); - assertThat(exception).hasMessage("@DisabledIf(\"#{'enigma'}\") on " + method + " must evaluate to \"true\" or \"false\", not \"enigma\""); + assertThatIllegalStateException() + .isThrownBy(() -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))) + .withMessageContaining( + "@DisabledIf(\"#{'enigma'}\") on " + method + " must evaluate to \"true\" or \"false\", not \"enigma\""); } @Test @@ -103,14 +98,16 @@ class DisabledIfConditionTests { void disabledWithDefaultReason() { ConditionEvaluationResult result = condition.evaluateExecutionCondition(buildExtensionContext("defaultReason")); assertThat(result.isDisabled()).isTrue(); - assertThat(result.getReason().get()).endsWith("defaultReason() is disabled because @DisabledIf(\"#{1 + 1 eq 2}\") evaluated to true"); + assertThat(result.getReason().get()) + .endsWith("defaultReason() is disabled because @DisabledIf(\"#{1 + 1 eq 2}\") evaluated to true"); } @Test void notDisabledWithDefaultReason() { ConditionEvaluationResult result = condition.evaluateExecutionCondition(buildExtensionContext("neverDisabledWithDefaultReason")); assertThat(result.isDisabled()).isFalse(); - assertThat(result.getReason().get()).endsWith("neverDisabledWithDefaultReason() is enabled because @DisabledIf(\"false\") did not evaluate to true"); + assertThat(result.getReason().get()) + .endsWith("neverDisabledWithDefaultReason() is enabled because @DisabledIf(\"false\") did not evaluate to true"); } // ------------------------------------------------------------------------- @@ -129,10 +126,9 @@ class DisabledIfConditionTests { } private void assertExpressionIsBlank(String methodName) { - IllegalStateException exception = assertThrows(IllegalStateException.class, - () -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))); - - assertThat(exception.getMessage()).contains("must not be blank"); + assertThatIllegalStateException() + .isThrownBy(() -> condition.evaluateExecutionCondition(buildExtensionContext(methodName))) + .withMessageContaining("must not be blank"); } // ------------------------------------------------------------------------- diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java index f1c706f54b9..1e3f61c3698 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java @@ -24,8 +24,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; /** * Integration tests which verify support for {@link DisabledIf @DisabledIf} @@ -111,7 +111,8 @@ class DisabledIfTests { @Test @DisabledOnMac void disabledIfWithSpelOsCheckInCustomComposedAnnotation() { - assertFalse(System.getProperty("os.name").contains("Mac"), "This test must be disabled on Mac OS"); + String os = System.getProperty("os.name").toLowerCase(); + assertThat(os).as("This test must be disabled on Mac OS").doesNotContain("mac"); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java index 576c6ada1b5..afccc93e8f2 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java @@ -24,9 +24,8 @@ import org.springframework.context.annotation.Configuration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; /** * Integration tests which verify support for {@link EnabledIf @EnabledIf} @@ -113,8 +112,8 @@ class EnabledIfTests { @EnabledOnMac void enabledIfWithSpelOsCheckInCustomComposedAnnotation() { String os = System.getProperty("os.name").toLowerCase(); - assertTrue(os.contains("mac"), "This test must be enabled on Mac OS"); - assertFalse(os.contains("win"), "This test must be disabled on Windows"); + assertThat(os).as("This test must be enabled on Mac OS").contains("mac"); + assertThat(os).as("This test must be disabled on Windows").doesNotContain("win"); } @Test diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java index 7778ec97555..77d5801fa40 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java @@ -31,7 +31,6 @@ import org.junit.platform.launcher.TestIdentifier; import org.junit.platform.launcher.core.LauncherFactory; import org.junit.platform.launcher.listeners.SummaryGeneratingListener; import org.junit.platform.launcher.listeners.TestExecutionSummary; -import org.opentest4j.AssertionFailedError; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -46,9 +45,9 @@ import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.Transactional; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; +import static org.assertj.core.api.SoftAssertions.assertSoftly; import static org.junit.jupiter.api.DynamicTest.dynamicTest; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; @@ -106,27 +105,20 @@ class FailingBeforeAndAfterMethodsSpringExtensionTests { int expectedSucceededCount = getExpectedSucceededCount(testClass); int expectedFailedCount = getExpectedFailedCount(testClass); - // @formatter:off - assertAll( - () -> assertEquals(1, summary.getTestsFoundCount(), () -> name + ": tests found"), - () -> assertEquals(0, summary.getTestsSkippedCount(), () -> name + ": tests skipped"), - () -> assertEquals(0, summary.getTestsAbortedCount(), () -> name + ": tests aborted"), - () -> assertEquals(expectedStartedCount, summary.getTestsStartedCount(), () -> name + ": tests started"), - () -> assertEquals(expectedSucceededCount, summary.getTestsSucceededCount(), () -> name + ": tests succeeded"), - () -> assertEquals(expectedFailedCount, summary.getTestsFailedCount(), () -> name + ": tests failed") - ); - // @formatter:on + assertSoftly(softly -> { + softly.assertThat(summary.getTestsFoundCount()).as("%s: tests found", name).isEqualTo(1); + softly.assertThat(summary.getTestsSkippedCount()).as("%s: tests skipped", name).isEqualTo(0); + softly.assertThat(summary.getTestsAbortedCount()).as("%s: tests aborted", name).isEqualTo(0); + softly.assertThat(summary.getTestsStartedCount()).as("%s: tests started", name).isEqualTo(expectedStartedCount); + softly.assertThat(summary.getTestsSucceededCount()).as("%s: tests succeeded", name).isEqualTo(expectedSucceededCount); + softly.assertThat(summary.getTestsFailedCount()).as("%s: tests failed", name).isEqualTo(expectedFailedCount); + }); - // Ensure it was an AssertionFailedError that failed the test and not + // Ensure it was an AssertionError that failed the test and not // something else like an error in the @Configuration class, etc. if (expectedFailedCount > 0) { - assertEquals(1, listener.exceptions.size(), "exceptions expected"); - Throwable exception = listener.exceptions.get(0); - if (!(exception instanceof AssertionFailedError)) { - throw new AssertionFailedError( - exception.getClass().getName() + " is not an instance of " + AssertionFailedError.class.getName(), - exception); - } + assertThat(listener.exceptions).as("exceptions expected").hasSize(1); + assertThat(listener.exceptions.get(0)).isInstanceOf(AssertionError.class); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/RegisterExtensionSpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/RegisterExtensionSpringExtensionTests.java index 5ae135318bf..ea9c627f8f5 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/RegisterExtensionSpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/RegisterExtensionSpringExtensionTests.java @@ -36,11 +36,7 @@ import org.springframework.test.context.junit.jupiter.comics.Cat; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate that the Spring TestContext Framework can be used @@ -82,56 +78,52 @@ class RegisterExtensionSpringExtensionTests { @Value("${enigma}") Integer enigma; + @Test void applicationContextInjectedIntoMethod(ApplicationContext applicationContext) { - assertNotNull(applicationContext, - "ApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test - void genericApplicationContextInjectedIntoMethod( - GenericApplicationContext applicationContext) { - assertNotNull(applicationContext, - "GenericApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + void genericApplicationContextInjectedIntoMethod(GenericApplicationContext applicationContext) { + assertThat(applicationContext).as("GenericApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test void autowiredFields() { - assertNotNull(this.dilbert, "Dilbert should have been @Autowired by Spring"); - assertEquals("Dilbert", this.dilbert.getName(), "Person's name"); - assertEquals(2, this.people.size(), "Number of people in context"); + assertThat(this.dilbert).as("Dilbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dilbert.getName()).as("Person's name").isEqualTo("Dilbert"); + assertThat(this.people).as("Number of people in context").hasSize(2); - assertNotNull(this.dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", this.dog.getName(), "Dog's name"); + assertThat(this.dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dog.getName()).as("Dog's name").isEqualTo("Dogbert"); - assertNotNull(this.cat, - "Catbert should have been @Autowired by Spring as the @Primary cat"); - assertEquals("Catbert", this.cat.getName(), "Primary cat's name"); - assertEquals(2, this.cats.size(), "Number of cats in context"); + assertThat(this.cat).as("Catbert should have been @Autowired by Spring as the @Primary cat").isNotNull(); + assertThat(this.cat.getName()).as("Primary cat's name").isEqualTo("Catbert"); + assertThat(this.cats).as("Number of cats in context").hasSize(2); - assertNotNull(this.enigma, - "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), this.enigma, "enigma"); + assertThat(this.enigma).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(this.enigma).as("enigma").isEqualTo(42); } @Test void autowiredParameterByTypeForSingleBean(@Autowired Dog dog) { - assertNotNull(dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", dog.getName(), "Dog's name"); + assertThat(dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(dog.getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test void autowiredParameterByTypeForPrimaryBean(@Autowired Cat primaryCat) { - assertNotNull(primaryCat, "Primary cat should have been @Autowired by Spring"); - assertEquals("Catbert", primaryCat.getName(), "Primary cat's name"); + assertThat(primaryCat).as("Primary cat should have been @Autowired by Spring").isNotNull(); + assertThat(primaryCat.getName()).as("Primary cat's name").isEqualTo("Catbert"); } @Test void autowiredParameterWithExplicitQualifier(@Qualifier("wally") Person person) { - assertNotNull(person, "Wally should have been @Autowired by Spring"); - assertEquals("Wally", person.getName(), "Person's name"); + assertThat(person).as("Wally should have been @Autowired by Spring").isNotNull(); + assertThat(person.getName()).as("Person's name").isEqualTo("Wally"); } /** @@ -140,85 +132,74 @@ class RegisterExtensionSpringExtensionTests { * {@code @Qualifier("wally")}. */ @Test - void autowiredParameterWithImplicitQualifierBasedOnParameterName( - @Autowired Person wally) { - assertNotNull(wally, "Wally should have been @Autowired by Spring"); - assertEquals("Wally", wally.getName(), "Person's name"); + void autowiredParameterWithImplicitQualifierBasedOnParameterName(@Autowired Person wally) { + assertThat(wally).as("Wally should have been @Autowired by Spring").isNotNull(); + assertThat(wally.getName()).as("Person's name").isEqualTo("Wally"); } @Test void autowiredParameterAsJavaUtilOptional(@Autowired Optional dog) { - assertNotNull(dog, "Optional dog should have been @Autowired by Spring"); - assertTrue(dog.isPresent(), "Value of Optional should be 'present'"); - assertEquals("Dogbert", dog.get().getName(), "Dog's name"); + assertThat(dog).as("Optional dog should have been @Autowired by Spring").isNotNull(); + assertThat(dog.isPresent()).as("Value of Optional should be 'present'").isTrue(); + assertThat(dog.get().getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test - void autowiredParameterThatDoesNotExistAsJavaUtilOptional( - @Autowired Optional number) { - assertNotNull(number, "Optional number should have been @Autowired by Spring"); - assertFalse(number.isPresent(), - "Value of Optional number should not be 'present'"); + void autowiredParameterThatDoesNotExistAsJavaUtilOptional(@Autowired Optional number) { + assertThat(number).as("Optional number should have been @Autowired by Spring").isNotNull(); + assertThat(number).as("Value of Optional number should not be 'present'").isNotPresent(); } @Test - void autowiredParameterThatDoesNotExistButIsNotRequired( - @Autowired(required = false) Number number) { - assertNull(number, - "Non-required number should have been @Autowired as 'null' by Spring"); + void autowiredParameterThatDoesNotExistButIsNotRequired(@Autowired(required = false) Number number) { + assertThat(number).as("Non-required number should have been @Autowired as 'null' by Spring").isNull(); } @Test void autowiredParameterOfList(@Autowired List peopleParam) { - assertNotNull(peopleParam, - "list of people should have been @Autowired by Spring"); - assertEquals(2, peopleParam.size(), "Number of people in context"); + assertThat(peopleParam).as("list of people should have been @Autowired by Spring").isNotNull(); + assertThat(peopleParam).as("Number of people in context").hasSize(2); } @Test void valueParameterWithPrimitiveType(@Value("99") int num) { - assertEquals(99, num); + assertThat(num).isEqualTo(99); } @Test void valueParameterFromPropertyPlaceholder(@Value("${enigma}") Integer enigmaParam) { - assertNotNull(enigmaParam, - "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), enigmaParam, "enigma"); + assertThat(enigmaParam).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(enigmaParam).as("enigma").isEqualTo(42); } @Test - void valueParameterFromDefaultValueForPropertyPlaceholder( - @Value("${bogus:false}") Boolean defaultValue) { - assertNotNull(defaultValue, - "Default value should have been injected via @Value by Spring"); - assertEquals(false, defaultValue, "default value"); + void valueParameterFromDefaultValueForPropertyPlaceholder(@Value("${bogus:false}") Boolean defaultValue) { + assertThat(defaultValue).as("Default value should have been injected via @Value by Spring").isNotNull(); + assertThat(defaultValue).as("default value").isEqualTo(false); } @Test void valueParameterFromSpelExpression(@Value("#{@dilbert.name}") String name) { - assertNotNull(name, - "Dilbert's name should have been injected via SpEL expression in @Value by Spring"); - assertEquals("Dilbert", name, "name from SpEL expression"); + assertThat(name).as( + "Dilbert's name should have been injected via SpEL expression in @Value by Spring").isNotNull(); + assertThat(name).as("name from SpEL expression").isEqualTo("Dilbert"); } @Test - void valueParameterFromSpelExpressionWithNestedPropertyPlaceholder( - @Value("#{'Hello ' + ${enigma}}") String hello) { - assertNotNull(hello, - "hello should have been injected via SpEL expression in @Value by Spring"); - assertEquals("Hello 42", hello, "hello from SpEL expression"); + void valueParameterFromSpelExpressionWithNestedPropertyPlaceholder(@Value("#{'Hello ' + ${enigma}}") String hello) { + assertThat(hello).as("hello should have been injected via SpEL expression in @Value by Spring").isNotNull(); + assertThat(hello).as("hello from SpEL expression").isEqualTo("Hello 42"); } @Test - void junitAndSpringMethodInjectionCombined(@Autowired Cat kittyCat, TestInfo testInfo, - ApplicationContext context, TestReporter testReporter) { + void junitAndSpringMethodInjectionCombined(@Autowired Cat kittyCat, TestInfo testInfo, ApplicationContext context, + TestReporter testReporter) { - assertNotNull(testInfo, "TestInfo should have been injected by JUnit"); - assertNotNull(testReporter, "TestReporter should have been injected by JUnit"); + assertThat(testInfo).as("TestInfo should have been injected by JUnit").isNotNull(); + assertThat(testReporter).as("TestReporter should have been injected by JUnit").isNotNull(); - assertNotNull(context, "ApplicationContext should have been injected by Spring"); - assertNotNull(kittyCat, "Cat should have been @Autowired by Spring"); + assertThat(context).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(kittyCat).as("Cat should have been @Autowired by Spring").isNotNull(); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java index 037cb5cf75f..ac3cdaa0f34 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java @@ -29,8 +29,7 @@ import org.springframework.test.context.junit.jupiter.comics.Cat; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate that the Spring TestContext Framework @@ -51,19 +50,19 @@ class SpringExtensionParameterizedTests { @ParameterizedTest @ValueSource(strings = { "Dilbert", "Wally" }) void people(String name, @Autowired List people) { - assertTrue(people.stream().map(Person::getName).filter(str -> name.equals(str)).findFirst().isPresent()); + assertThat(people.stream().map(Person::getName).filter(name::equals)).size().isEqualTo(1); } @ParameterizedTest @CsvSource("dogbert, Dogbert") void dogs(String beanName, String dogName, ApplicationContext context) { - assertEquals(dogName, context.getBean(beanName, Dog.class).getName()); + assertThat(context.getBean(beanName, Dog.class)).extracting(Dog::getName).isEqualTo(dogName); } @ParameterizedTest @CsvSource({ "garfield, Garfield", "catbert, Catbert" }) void cats(String beanName, String catName, ApplicationContext context) { - assertEquals(catName, context.getBean(beanName, Cat.class).getName()); + assertThat(context.getBean(beanName, Cat.class)).extracting(Cat::getName).isEqualTo(catName); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionTests.java index c8ee3cd5fac..73e9acdd98f 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionTests.java @@ -36,11 +36,7 @@ import org.springframework.test.context.junit.jupiter.comics.Cat; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate that the Spring TestContext Framework can @@ -79,49 +75,49 @@ class SpringExtensionTests { @Test void applicationContextInjectedIntoMethod(ApplicationContext applicationContext) { - assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test void genericApplicationContextInjectedIntoMethod(GenericApplicationContext applicationContext) { - assertNotNull(applicationContext, "GenericApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("GenericApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test void autowiredFields() { - assertNotNull(this.dilbert, "Dilbert should have been @Autowired by Spring"); - assertEquals("Dilbert", this.dilbert.getName(), "Person's name"); - assertEquals(2, this.people.size(), "Number of people in context"); + assertThat(this.dilbert).as("Dilbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dilbert.getName()).as("Person's name").isEqualTo("Dilbert"); + assertThat(this.people).as("Number of people in context").hasSize(2); - assertNotNull(this.dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", this.dog.getName(), "Dog's name"); + assertThat(this.dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dog.getName()).as("Dog's name").isEqualTo("Dogbert"); - assertNotNull(this.cat, "Catbert should have been @Autowired by Spring as the @Primary cat"); - assertEquals("Catbert", this.cat.getName(), "Primary cat's name"); - assertEquals(2, this.cats.size(), "Number of cats in context"); + assertThat(this.cat).as("Catbert should have been @Autowired by Spring as the @Primary cat").isNotNull(); + assertThat(this.cat.getName()).as("Primary cat's name").isEqualTo("Catbert"); + assertThat(this.cats).as("Number of cats in context").hasSize(2); - assertNotNull(this.enigma, "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), this.enigma, "enigma"); + assertThat(this.enigma).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(this.enigma).as("enigma").isEqualTo(42); } @Test void autowiredParameterByTypeForSingleBean(@Autowired Dog dog) { - assertNotNull(dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", dog.getName(), "Dog's name"); + assertThat(dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(dog.getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test void autowiredParameterByTypeForPrimaryBean(@Autowired Cat primaryCat) { - assertNotNull(primaryCat, "Primary cat should have been @Autowired by Spring"); - assertEquals("Catbert", primaryCat.getName(), "Primary cat's name"); + assertThat(primaryCat).as("Primary cat should have been @Autowired by Spring").isNotNull(); + assertThat(primaryCat.getName()).as("Primary cat's name").isEqualTo("Catbert"); } @Test void autowiredParameterWithExplicitQualifier(@Qualifier("wally") Person person) { - assertNotNull(person, "Wally should have been @Autowired by Spring"); - assertEquals("Wally", person.getName(), "Person's name"); + assertThat(person).as("Wally should have been @Autowired by Spring").isNotNull(); + assertThat(person.getName()).as("Person's name").isEqualTo("Wally"); } /** @@ -131,72 +127,72 @@ class SpringExtensionTests { */ @Test void autowiredParameterWithImplicitQualifierBasedOnParameterName(@Autowired Person wally) { - assertNotNull(wally, "Wally should have been @Autowired by Spring"); - assertEquals("Wally", wally.getName(), "Person's name"); + assertThat(wally).as("Wally should have been @Autowired by Spring").isNotNull(); + assertThat(wally.getName()).as("Person's name").isEqualTo("Wally"); } @Test void autowiredParameterAsJavaUtilOptional(@Autowired Optional dog) { - assertNotNull(dog, "Optional dog should have been @Autowired by Spring"); - assertTrue(dog.isPresent(), "Value of Optional should be 'present'"); - assertEquals("Dogbert", dog.get().getName(), "Dog's name"); + assertThat(dog).as("Optional dog should have been @Autowired by Spring").isNotNull(); + assertThat(dog).as("Value of Optional should be 'present'").isPresent(); + assertThat(dog.get().getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test void autowiredParameterThatDoesNotExistAsJavaUtilOptional(@Autowired Optional number) { - assertNotNull(number, "Optional number should have been @Autowired by Spring"); - assertFalse(number.isPresent(), "Value of Optional number should not be 'present'"); + assertThat(number).as("Optional number should have been @Autowired by Spring").isNotNull(); + assertThat(number).as("Value of Optional number should not be 'present'").isNotPresent(); } @Test void autowiredParameterThatDoesNotExistButIsNotRequired(@Autowired(required = false) Number number) { - assertNull(number, "Non-required number should have been @Autowired as 'null' by Spring"); + assertThat(number).as("Non-required number should have been @Autowired as 'null' by Spring").isNull(); } @Test void autowiredParameterOfList(@Autowired List peopleParam) { - assertNotNull(peopleParam, "list of people should have been @Autowired by Spring"); - assertEquals(2, peopleParam.size(), "Number of people in context"); + assertThat(peopleParam).as("list of people should have been @Autowired by Spring").isNotNull(); + assertThat(peopleParam).as("Number of people in context").hasSize(2); } @Test void valueParameterWithPrimitiveType(@Value("99") int num) { - assertEquals(99, num); + assertThat(num).isEqualTo(99); } @Test void valueParameterFromPropertyPlaceholder(@Value("${enigma}") Integer enigmaParam) { - assertNotNull(enigmaParam, "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), enigmaParam, "enigma"); + assertThat(enigmaParam).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(enigmaParam).as("enigma").isEqualTo(42); } @Test void valueParameterFromDefaultValueForPropertyPlaceholder(@Value("${bogus:false}") Boolean defaultValue) { - assertNotNull(defaultValue, "Default value should have been injected via @Value by Spring"); - assertEquals(false, defaultValue, "default value"); + assertThat(defaultValue).as("Default value should have been injected via @Value by Spring").isNotNull(); + assertThat(defaultValue).as("default value").isEqualTo(false); } @Test void valueParameterFromSpelExpression(@Value("#{@dilbert.name}") String name) { - assertNotNull(name, "Dilbert's name should have been injected via SpEL expression in @Value by Spring"); - assertEquals("Dilbert", name, "name from SpEL expression"); + assertThat(name).as("Dilbert's name should have been injected via SpEL expression in @Value by Spring").isNotNull(); + assertThat(name).as("name from SpEL expression").isEqualTo("Dilbert"); } @Test void valueParameterFromSpelExpressionWithNestedPropertyPlaceholder(@Value("#{'Hello ' + ${enigma}}") String hello) { - assertNotNull(hello, "hello should have been injected via SpEL expression in @Value by Spring"); - assertEquals("Hello 42", hello, "hello from SpEL expression"); + assertThat(hello).as("hello should have been injected via SpEL expression in @Value by Spring").isNotNull(); + assertThat(hello).as("hello from SpEL expression").isEqualTo("Hello 42"); } @Test void junitAndSpringMethodInjectionCombined(@Autowired Cat kittyCat, TestInfo testInfo, ApplicationContext context, TestReporter testReporter) { - assertNotNull(testInfo, "TestInfo should have been injected by JUnit"); - assertNotNull(testReporter, "TestReporter should have been injected by JUnit"); + assertThat(testInfo).as("TestInfo should have been injected by JUnit").isNotNull(); + assertThat(testReporter).as("TestReporter should have been injected by JUnit").isNotNull(); - assertNotNull(context, "ApplicationContext should have been injected by Spring"); - assertNotNull(kittyCat, "Cat should have been @Autowired by Spring"); + assertThat(context).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(kittyCat).as("Cat should have been @Autowired by Spring").isNotNull(); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterAutowiredConstructorInjectionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterAutowiredConstructorInjectionTests.java index 4fe1636d476..7796587d3f3 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterAutowiredConstructorInjectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterAutowiredConstructorInjectionTests.java @@ -26,8 +26,7 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate support for {@link Autowired @Autowired} @@ -62,23 +61,23 @@ class SpringJUnitJupiterAutowiredConstructorInjectionTests { @Test void applicationContextInjected() { - assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test void beansInjected() { - assertNotNull(this.dilbert, "Dilbert should have been @Autowired by Spring"); - assertEquals("Dilbert", this.dilbert.getName(), "Person's name"); + assertThat(this.dilbert).as("Dilbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dilbert.getName()).as("Person's name").isEqualTo("Dilbert"); - assertNotNull(this.dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", this.dog.getName(), "Dog's name"); + assertThat(this.dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dog.getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test void propertyPlaceholderInjected() { - assertNotNull(this.enigma, "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), this.enigma, "enigma"); + assertThat(this.enigma).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(this.enigma).as("enigma").isEqualTo(42); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterConstructorInjectionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterConstructorInjectionTests.java index 0d0b25d1e6d..029bda128ff 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterConstructorInjectionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterConstructorInjectionTests.java @@ -27,8 +27,7 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate support for autowiring individual @@ -65,28 +64,28 @@ class SpringJUnitJupiterConstructorInjectionTests { @Test void applicationContextInjected() { - assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test void beansInjected() { - assertNotNull(this.dilbert, "Dilbert should have been @Autowired by Spring"); - assertEquals("Dilbert", this.dilbert.getName(), "Person's name"); + assertThat(this.dilbert).as("Dilbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dilbert.getName()).as("Person's name").isEqualTo("Dilbert"); - assertNotNull(this.dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", this.dog.getName(), "Dog's name"); + assertThat(this.dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dog.getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test void propertyPlaceholderInjected() { - assertNotNull(this.enigma, "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), this.enigma, "enigma"); + assertThat(this.enigma).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(this.enigma).as("enigma").isEqualTo(42); } @Test void testInfoInjected() { - assertNotNull(this.testInfo, "TestInfo should have been injected by JUnit"); + assertThat(this.testInfo).as("TestInfo should have been injected by JUnit").isNotNull(); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringManagedJupiterExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringManagedJupiterExtensionTests.java index f9e1b6c5b0e..b1b52700362 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringManagedJupiterExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringManagedJupiterExtensionTests.java @@ -35,7 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * This class demonstrates how to have a JUnit Jupiter extension managed as a @@ -56,7 +56,7 @@ class SpringManagedJupiterExtensionTests { @TestTemplate void testTemplate(String parameter) { - assertTrue("foo".equals(parameter) || "bar".equals(parameter)); + assertThat("foo".equals(parameter) || "bar".equals(parameter)).isTrue(); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorAnnotationIntegrationTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorAnnotationIntegrationTests.java index d6a479c7e44..6c5f12bf74d 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorAnnotationIntegrationTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorAnnotationIntegrationTests.java @@ -27,8 +27,7 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; import org.springframework.test.context.junit.jupiter.comics.Dog; import org.springframework.test.context.junit.jupiter.comics.Person; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests which demonstrate support for automatically @@ -65,23 +64,23 @@ class TestConstructorAnnotationIntegrationTests { @Test void applicationContextInjected() { - assertNotNull(applicationContext, "ApplicationContext should have been injected by Spring"); - assertEquals(this.dilbert, applicationContext.getBean("dilbert", Person.class)); + assertThat(applicationContext).as("ApplicationContext should have been injected by Spring").isNotNull(); + assertThat(applicationContext.getBean("dilbert", Person.class)).isEqualTo(this.dilbert); } @Test void beansInjected() { - assertNotNull(this.dilbert, "Dilbert should have been @Autowired by Spring"); - assertEquals("Dilbert", this.dilbert.getName(), "Person's name"); + assertThat(this.dilbert).as("Dilbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dilbert.getName()).as("Person's name").isEqualTo("Dilbert"); - assertNotNull(this.dog, "Dogbert should have been @Autowired by Spring"); - assertEquals("Dogbert", this.dog.getName(), "Dog's name"); + assertThat(this.dog).as("Dogbert should have been @Autowired by Spring").isNotNull(); + assertThat(this.dog.getName()).as("Dog's name").isEqualTo("Dogbert"); } @Test void propertyPlaceholderInjected() { - assertNotNull(this.enigma, "Enigma should have been injected via @Value by Spring"); - assertEquals(Integer.valueOf(42), this.enigma, "enigma"); + assertThat(this.enigma).as("Enigma should have been injected via @Value by Spring").isNotNull(); + assertThat(this.enigma).as("enigma").isEqualTo(42); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/defaultmethods/GenericComicCharactersInterfaceDefaultMethodsTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/defaultmethods/GenericComicCharactersInterfaceDefaultMethodsTests.java index 55b568ebecf..3fc606cf726 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/defaultmethods/GenericComicCharactersInterfaceDefaultMethodsTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/defaultmethods/GenericComicCharactersInterfaceDefaultMethodsTests.java @@ -27,8 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.junit.jupiter.TestConfig; import org.springframework.test.context.junit.jupiter.comics.Character; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Interface for integration tests that demonstrate support for interface default @@ -46,13 +45,13 @@ interface GenericComicCharactersInterfaceDefaultMethodsTests characters) { - assertEquals(getExpectedNumCharacters(), characters.size(), "Number of characters in context"); + assertThat(characters).as("Number of characters in context").size().isEqualTo(getExpectedNumCharacters()); } @Test default void autowiredParameterWithGenericBean(@Autowired C character) { - assertNotNull(character, "Character should have been @Autowired by Spring"); - assertEquals(getExpectedName(), character.getName(), "character's name"); + assertThat(character).as("Character should have been @Autowired by Spring").isNotNull(); + assertThat(character).as("character's name").extracting(Character::getName).isEqualTo(getExpectedName()); } int getExpectedNumCharacters(); diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/generics/GenericComicCharactersTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/generics/GenericComicCharactersTests.java index 668b29885ff..dd2180d7529 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/generics/GenericComicCharactersTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/generics/GenericComicCharactersTests.java @@ -27,8 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.junit.jupiter.TestConfig; import org.springframework.test.context.junit.jupiter.comics.Character; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.assertj.core.api.Assertions.assertThat; /** * Abstract base class for integration tests that demonstrate support for @@ -52,15 +51,15 @@ abstract class GenericComicCharactersTests { @Test void autowiredFields() { - assertNotNull(this.character, "Character should have been @Autowired by Spring"); - assertEquals(getExpectedName(), character.getName(), "character's name"); - assertEquals(getExpectedNumCharacters(), this.characters.size(), "Number of characters in context"); + assertThat(this.character).as("Character should have been @Autowired by Spring").isNotNull(); + assertThat(this.character).as("character's name").extracting(Character::getName).isEqualTo(getExpectedName()); + assertThat(this.characters).as("Number of characters in context").size().isEqualTo(getExpectedNumCharacters()); } @Test void autowiredParameterByTypeForSingleGenericBean(@Autowired T character) { - assertNotNull(character, "Character should have been @Autowired by Spring"); - assertEquals(getExpectedName(), character.getName(), "character's name"); + assertThat(character).as("Character should have been @Autowired by Spring").isNotNull(); + assertThat(this.character).as("character's name").extracting(Character::getName).isEqualTo(getExpectedName()); } abstract int getExpectedNumCharacters(); diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests.java index 68cf169e6dc..6478eca91b9 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests.java @@ -30,7 +30,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.junit.jupiter.nested.NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests.TopLevelConfig; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests that verify support for {@code @Nested} test classes in conjunction @@ -57,7 +57,7 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests { @Test void topLevelTest() { - assertEquals("foo", foo); + assertThat(foo).isEqualTo("foo"); } @Nested @@ -73,8 +73,8 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests { @Test void nestedTest() throws Exception { - assertEquals("foo", foo); - assertEquals("bar", bar); + assertThat(foo).isEqualTo("foo"); + assertThat(bar).isEqualTo("bar"); } } @@ -90,8 +90,8 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests { @Test void nestedTest() throws Exception { - assertEquals("foo", foo); - assertEquals("bar", bar); + assertThat(foo).isEqualTo("foo"); + assertThat(bar).isEqualTo("bar"); } } @@ -107,8 +107,8 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests { @Test void nestedTest() throws Exception { - assertEquals("foo", foo); - assertEquals("bar", bar); + assertThat(foo).isEqualTo("foo"); + assertThat(bar).isEqualTo("bar"); } } @@ -126,9 +126,9 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests { @Test void nestedTest() throws Exception { - assertEquals("foo", foo); - assertEquals("bar", bar); - assertEquals(42, answer); + assertThat(foo).isEqualTo("foo"); + assertThat(bar).isEqualTo("bar"); + assertThat(answer).isEqualTo(42); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSpringAndJUnitJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSpringAndJUnitJupiterTests.java index 8165ae7feb2..b6a8c554fd3 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSpringAndJUnitJupiterTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSpringAndJUnitJupiterTests.java @@ -27,7 +27,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; import org.springframework.test.context.junit.jupiter.nested.NestedTestsWithSpringAndJUnitJupiterTests.TopLevelConfig; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests that verify support for {@code @Nested} test classes @@ -50,7 +50,7 @@ class NestedTestsWithSpringAndJUnitJupiterTests { @Test void topLevelTest() { - assertEquals("foo", foo); + assertThat(foo).isEqualTo("foo"); } @@ -67,8 +67,8 @@ class NestedTestsWithSpringAndJUnitJupiterTests { // In contrast to nested test classes running in JUnit 4, the foo // field in the outer instance should have been injected from the // test ApplicationContext for the outer instance. - assertEquals("foo", foo); - assertEquals("bar", bar); + assertThat(foo).isEqualTo("foo"); + assertThat(bar).isEqualTo("bar"); } } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSqlScriptsAndJUnitJupiterTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSqlScriptsAndJUnitJupiterTests.java index c8479744d2e..d0174f06e54 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSqlScriptsAndJUnitJupiterTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSqlScriptsAndJUnitJupiterTests.java @@ -32,7 +32,7 @@ import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.test.jdbc.JdbcTestUtils; import org.springframework.transaction.annotation.Transactional; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** * Integration tests that verify support for {@link Nested @Nested} test classes in @@ -53,13 +53,13 @@ class NestedTestsWithSqlScriptsAndJUnitJupiterTests { @BeforeTransaction @AfterTransaction void checkInitialDatabaseState() { - assertEquals(0, countRowsInTable("user")); + assertThat(countRowsInTable("user")).isEqualTo(0); } @Test @Sql("/org/springframework/test/context/jdbc/data.sql") void sqlScripts() { - assertEquals(1, countRowsInTable("user")); + assertThat(countRowsInTable("user")).isEqualTo(1); } private int countRowsInTable(String tableName) { @@ -77,13 +77,13 @@ class NestedTestsWithSqlScriptsAndJUnitJupiterTests { @BeforeTransaction @AfterTransaction void checkInitialDatabaseState() { - assertEquals(0, countRowsInTable("user")); + assertThat(countRowsInTable("user")).isEqualTo(0); } @Test @Sql("/org/springframework/test/context/jdbc/data.sql") void nestedSqlScripts() { - assertEquals(1, countRowsInTable("user")); + assertThat(countRowsInTable("user")).isEqualTo(1); } private int countRowsInTable(String tableName) { diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/parallel/ParallelExecutionSpringExtensionTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/parallel/ParallelExecutionSpringExtensionTests.java index 4f019778ad6..6518d2a2541 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/parallel/ParallelExecutionSpringExtensionTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/parallel/ParallelExecutionSpringExtensionTests.java @@ -31,7 +31,7 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Configuration; import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass; import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.request; @@ -61,8 +61,8 @@ class ParallelExecutionSpringExtensionTests { launcher.execute(request); - assertEquals(NUM_TESTS, listener.getSummary().getTestsSucceededCount(), - "number of tests executed successfully"); + assertThat(listener.getSummary().getTestsSucceededCount()).as( + "number of tests executed successfully").isEqualTo(NUM_TESTS); } @SpringJUnitConfig diff --git a/spring-test/src/test/java/org/springframework/test/context/junit4/JUnitTestingUtils.java b/spring-test/src/test/java/org/springframework/test/context/junit4/JUnitTestingUtils.java index 794a26b1245..75643a19c04 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit4/JUnitTestingUtils.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit4/JUnitTestingUtils.java @@ -19,7 +19,6 @@ package org.springframework.test.context.junit4; import java.lang.reflect.Constructor; import org.junit.experimental.ParallelComputer; -import org.junit.jupiter.api.Assertions; import org.junit.runner.Computer; import org.junit.runner.JUnitCore; import org.junit.runner.RunWith; @@ -28,16 +27,11 @@ import org.junit.runner.notification.RunNotifier; import org.springframework.beans.BeanUtils; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.assertj.core.api.SoftAssertions.assertSoftly; /** * Collection of utilities for testing the execution of JUnit 4 based tests. * - *

Note that these utilities use {@link Assertions} from JUnit Jupiter, - * but that should not result in any adverse side effects in terms of - * proper test failure for failed assertions. - * * @author Sam Brannen * @since 4.2 * @see TrackingRunListener @@ -101,15 +95,18 @@ public class JUnitTestingUtils { junit.run(testClass); } - // @formatter:off - assertAll( - () -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started for [" + testClass + "]"), - () -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed for [" + testClass + "]"), - () -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished for [" + testClass + "]"), - () -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored for [" + testClass + "]"), - () -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions for [" + testClass + "]") - ); - // @formatter:on + assertSoftly(softly -> { + softly.assertThat(listener.getTestStartedCount()).as("tests started for [%s]", testClass) + .isEqualTo(expectedStartedCount); + softly.assertThat(listener.getTestFailureCount()).as("tests failed for [%s]", testClass) + .isEqualTo(expectedFailedCount); + softly.assertThat(listener.getTestFinishedCount()).as("tests finished for [%s]", testClass) + .isEqualTo(expectedFinishedCount); + softly.assertThat(listener.getTestIgnoredCount()).as("tests ignored for [%s]", testClass) + .isEqualTo(expectedIgnoredCount); + softly.assertThat(listener.getTestAssumptionFailureCount()).as("failed assumptions for [%s]", testClass) + .isEqualTo(expectedAssumptionFailedCount); + }); } /** @@ -140,15 +137,13 @@ public class JUnitTestingUtils { junit.addListener(listener); junit.run(computer, testClasses); - // @formatter:off - assertAll( - () -> assertEquals(expectedStartedCount, listener.getTestStartedCount(), "tests started"), - () -> assertEquals(expectedFailedCount, listener.getTestFailureCount(), "tests failed"), - () -> assertEquals(expectedFinishedCount, listener.getTestFinishedCount(), "tests finished"), - () -> assertEquals(expectedIgnoredCount, listener.getTestIgnoredCount(), "tests ignored"), - () -> assertEquals(expectedAssumptionFailedCount, listener.getTestAssumptionFailureCount(), "failed assumptions") - ); - // @formatter:on + assertSoftly(softly -> { + softly.assertThat(listener.getTestStartedCount()).as("tests started]").isEqualTo(expectedStartedCount); + softly.assertThat(listener.getTestFailureCount()).as("tests failed]").isEqualTo(expectedFailedCount); + softly.assertThat(listener.getTestFinishedCount()).as("tests finished]").isEqualTo(expectedFinishedCount); + softly.assertThat(listener.getTestIgnoredCount()).as("tests ignored]").isEqualTo(expectedIgnoredCount); + softly.assertThat(listener.getTestAssumptionFailureCount()).as("failed assumptions]").isEqualTo(expectedAssumptionFailedCount); + }); } } diff --git a/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java index 060f53c6e73..0e8a999dc51 100644 --- a/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java @@ -25,9 +25,8 @@ import org.junit.Test; import org.springframework.http.HttpMethod; import org.springframework.mock.http.client.MockClientHttpRequest; -import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.hamcrest.Matchers.containsString; -import static org.junit.jupiter.api.Assertions.assertThrows; /** * Unit tests for {@link MockRestRequestMatchers}. @@ -59,8 +58,9 @@ public class MockRestRequestMatchersTests { public void requestToNoMatch() throws Exception { this.request.setURI(new URI("http://www.foo.com/bar")); - assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.requestTo("http://www.foo.com/wrong").match(this.request)); + assertThatThrownBy( + () -> MockRestRequestMatchers.requestTo("http://www.foo.com/wrong").match(this.request)) + .isInstanceOf(AssertionError.class); } @Test @@ -81,9 +81,9 @@ public class MockRestRequestMatchersTests { public void methodNoMatch() throws Exception { this.request.setMethod(HttpMethod.POST); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)); - assertThat(error.getMessage()).contains("expected: but was:"); + assertThatThrownBy(() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("expected: but was:"); } @Test @@ -95,18 +95,18 @@ public class MockRestRequestMatchersTests { @Test public void headerMissing() throws Exception { - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); - assertThat(error.getMessage()).contains("was null"); + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("was null"); } @Test public void headerMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.header("foo", "bad").match(this.request)); - assertThat(error.getMessage()).contains("expected: but was:"); + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bad").match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("expected: but was:"); } @Test @@ -118,18 +118,18 @@ public class MockRestRequestMatchersTests { @Test public void headerContainsWithMissingHeader() throws Exception { - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)); - assertThat(error.getMessage()).contains("but was null"); + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("baz")).match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("but was null"); } @Test public void headerContainsWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Arrays.asList("bar", "baz")); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)); - assertThat(error.getMessage()).contains("was \"bar\""); + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", containsString("bx")).match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("was \"bar\""); } @Test @@ -141,18 +141,18 @@ public class MockRestRequestMatchersTests { @Test public void headersWithMissingHeader() throws Exception { - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.header("foo", "bar").match(this.request)); - assertThat(error.getMessage()).contains("but was null"); + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("but was null"); } @Test public void headersWithMissingValue() throws Exception { this.request.getHeaders().put("foo", Collections.singletonList("bar")); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)); - assertThat(error.getMessage()).contains("to have at least <2> values"); + assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("to have at least <2> values"); } @Test @@ -166,18 +166,18 @@ public class MockRestRequestMatchersTests { public void queryParamMissing() throws Exception { this.request.setURI(new URI("http://www.foo.com/a")); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request)); - assertThat(error.getMessage()).contains("but was null"); + assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bar").match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("but was null"); } @Test public void queryParamMissingValue() throws Exception { this.request.setURI(new URI("http://www.foo.com/a?foo=bar&foo=baz")); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request)); - assertThat(error.getMessage()).contains("expected: but was:"); + assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("expected: but was:"); } @Test @@ -191,9 +191,9 @@ public class MockRestRequestMatchersTests { public void queryParamContainsWithMissingValue() throws Exception { this.request.setURI(new URI("http://www.foo.com/a?foo=bar&foo=baz")); - AssertionError error = assertThrows(AssertionError.class, - () -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request)); - assertThat(error.getMessage()).contains("was \"bar\""); + assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", containsString("bx")).match(this.request)) + .isInstanceOf(AssertionError.class) + .hasMessageContaining("was \"bar\""); } } diff --git a/src/checkstyle/checkstyle.xml b/src/checkstyle/checkstyle.xml index fffa8446eae..fb6eca4eed5 100644 --- a/src/checkstyle/checkstyle.xml +++ b/src/checkstyle/checkstyle.xml @@ -98,7 +98,7 @@ + value="^reactor\.core\.support\.Assert,^junit\.framework\..+,^org\.junit\.Assert$,^org\.junit\.Assume$,^org\.junit\.jupiter\.api\.Assertions$,^org\.junit\.Assert\.assertThat,^org\.junit\.Assume\.assumeThat,^org\.junit\.rules\.ExpectedException,^org\.slf4j\.LoggerFactory" /> @@ -177,15 +177,21 @@ - + + - + + + + + + + +