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