Browse Source

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
pull/23031/head
Sam Brannen 7 years ago
parent
commit
5a1217cafe
  1. 13
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/ComposedSpringExtensionTests.java
  2. 42
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfConditionTests.java
  3. 7
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java
  4. 9
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java
  5. 36
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java
  6. 127
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/RegisterExtensionSpringExtensionTests.java
  7. 9
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionParameterizedTests.java
  8. 92
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringExtensionTests.java
  9. 19
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterAutowiredConstructorInjectionTests.java
  10. 21
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterConstructorInjectionTests.java
  11. 4
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringManagedJupiterExtensionTests.java
  12. 19
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorAnnotationIntegrationTests.java
  13. 9
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/defaultmethods/GenericComicCharactersInterfaceDefaultMethodsTests.java
  14. 13
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/generics/GenericComicCharactersTests.java
  15. 22
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests.java
  16. 8
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSpringAndJUnitJupiterTests.java
  17. 10
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/NestedTestsWithSqlScriptsAndJUnitJupiterTests.java
  18. 6
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/parallel/ParallelExecutionSpringExtensionTests.java
  19. 45
      spring-test/src/test/java/org/springframework/test/context/junit4/JUnitTestingUtils.java
  20. 68
      spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java
  21. 16
      src/checkstyle/checkstyle.xml

13
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/ComposedSpringExtensionTests.java

@ -28,8 +28,7 @@ import org.springframework.test.context.ContextConfiguration; @@ -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 { @@ -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);
}
}

42
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfConditionTests.java

@ -30,11 +30,10 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; @@ -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 { @@ -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 { @@ -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 { @@ -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");
}
// -------------------------------------------------------------------------

7
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/DisabledIfTests.java

@ -24,8 +24,8 @@ import org.springframework.context.annotation.Configuration; @@ -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 { @@ -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

9
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/EnabledIfTests.java

@ -24,9 +24,8 @@ import org.springframework.context.annotation.Configuration; @@ -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 { @@ -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

36
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/FailingBeforeAndAfterMethodsSpringExtensionTests.java

@ -31,7 +31,6 @@ import org.junit.platform.launcher.TestIdentifier; @@ -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; @@ -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 { @@ -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);
}
}

127
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; @@ -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 { @@ -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 { @@ -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> 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> 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> 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<Person> 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();
}
}

9
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; @@ -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 { @@ -51,19 +50,19 @@ class SpringExtensionParameterizedTests {
@ParameterizedTest
@ValueSource(strings = { "Dilbert", "Wally" })
void people(String name, @Autowired List<Person> 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);
}
}

92
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; @@ -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 { @@ -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 { @@ -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> 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> 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<Person> 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();
}
}

19
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterAutowiredConstructorInjectionTests.java

@ -26,8 +26,7 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; @@ -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 { @@ -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);
}
}

21
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringJUnitJupiterConstructorInjectionTests.java

@ -27,8 +27,7 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; @@ -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 { @@ -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();
}
}

4
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/SpringManagedJupiterExtensionTests.java

@ -35,7 +35,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -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 { @@ -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();
}

19
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/TestConstructorAnnotationIntegrationTests.java

@ -27,8 +27,7 @@ import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite; @@ -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 { @@ -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);
}
}

9
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; @@ -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<C extends Character @@ -46,13 +45,13 @@ interface GenericComicCharactersInterfaceDefaultMethodsTests<C extends Character
@Test
default void autowiredParameterWithParameterizedList(@Autowired List<C> 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();

13
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; @@ -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<T extends Character> { @@ -52,15 +51,15 @@ abstract class GenericComicCharactersTests<T extends Character> {
@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();

22
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; @@ -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 { @@ -57,7 +57,7 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests {
@Test
void topLevelTest() {
assertEquals("foo", foo);
assertThat(foo).isEqualTo("foo");
}
@Nested
@ -73,8 +73,8 @@ class NestedTestsWithConstructorInjectionWithSpringAndJUnitJupiterTests { @@ -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 { @@ -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 { @@ -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 { @@ -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);
}
}

8
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; @@ -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 { @@ -50,7 +50,7 @@ class NestedTestsWithSpringAndJUnitJupiterTests {
@Test
void topLevelTest() {
assertEquals("foo", foo);
assertThat(foo).isEqualTo("foo");
}
@ -67,8 +67,8 @@ class NestedTestsWithSpringAndJUnitJupiterTests { @@ -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");
}
}

10
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; @@ -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 { @@ -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 { @@ -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) {

6
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/parallel/ParallelExecutionSpringExtensionTests.java

@ -31,7 +31,7 @@ import org.springframework.context.ApplicationContext; @@ -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 { @@ -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

45
spring-test/src/test/java/org/springframework/test/context/junit4/JUnitTestingUtils.java

@ -19,7 +19,6 @@ package org.springframework.test.context.junit4; @@ -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; @@ -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.
*
* <p>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 { @@ -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 { @@ -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);
});
}
}

68
spring-test/src/test/java/org/springframework/test/web/client/match/MockRestRequestMatchersTests.java

@ -25,9 +25,8 @@ import org.junit.Test; @@ -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 { @@ -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 { @@ -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:<GET> but was:<POST>");
assertThatThrownBy(() -> MockRestRequestMatchers.method(HttpMethod.GET).match(this.request))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("expected:<GET> but was:<POST>");
}
@Test
@ -95,18 +95,18 @@ public class MockRestRequestMatchersTests { @@ -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:<bad> but was:<bar>");
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bad").match(this.request))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("expected:<bad> but was:<bar>");
}
@Test
@ -118,18 +118,18 @@ public class MockRestRequestMatchersTests { @@ -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 { @@ -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 { @@ -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:<bad> but was:<bar>");
assertThatThrownBy(() -> MockRestRequestMatchers.queryParam("foo", "bad").match(this.request))
.isInstanceOf(AssertionError.class)
.hasMessageContaining("expected:<bad> but was:<bar>");
}
@Test
@ -191,9 +191,9 @@ public class MockRestRequestMatchersTests { @@ -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\"");
}
}

16
src/checkstyle/checkstyle.xml

@ -98,7 +98,7 @@ @@ -98,7 +98,7 @@
<property name="id" value="bannedImports"/>
<property name="regexp" value="true" />
<property name="illegalClasses"
value="^reactor\.core\.support\.Assert,^junit\.framework\..+,^org\.junit\.Assert$,^org\.junit\.Assume$,^org\.junit\.Assert\.assertThat,^org\.junit\.Assume\.assumeThat,^org\.junit\.rules\.ExpectedException,^org\.slf4j\.LoggerFactory" />
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" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.imports.IllegalImportCheck">
<property name="id" value="bannedHamcrestImports"/>
@ -177,15 +177,21 @@ @@ -177,15 +177,21 @@
<property name="id" value="expectedExceptionAnnotation"/>
<property name="maximum" value="0"/>
<property name="format" value="\@Test\(expected" />
<property name="message"
value="Please use AssertJ asserThat." />
<property name="message" value="Please use AssertJ assertions." />
<property name="ignoreComments" value="true" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="junit4Assertions"/>
<property name="maximum" value="0"/>
<property name="format" value="org\.junit\.Assert\.assert" />
<property name="message"
value="Please use AssertJ imports." />
<property name="message" value="Please use AssertJ assertions." />
<property name="ignoreComments" value="true" />
</module>
<module name="com.puppycrawl.tools.checkstyle.checks.regexp.RegexpSinglelineJavaCheck">
<property name="id" value="junitJupiterAssertions"/>
<property name="maximum" value="0"/>
<property name="format" value="org\.junit\.jupiter\.api\.Assertions\.assert" />
<property name="message" value="Please use AssertJ assertions." />
<property name="ignoreComments" value="true" />
</module>

Loading…
Cancel
Save