diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/Person.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/Person.java new file mode 100644 index 0000000000..f0fd0e955e --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/Person.java @@ -0,0 +1,11 @@ +package com.baeldung.springjunitconfiguration; + +public class Person { + + String name; + + public Person(String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitConfigIntegrationTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitConfigIntegrationTest.java new file mode 100644 index 0000000000..864eba97cc --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitConfigIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.springjunitconfiguration; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +import static org.junit.Assert.assertNotNull; + +/** + * @SpringJUnitConfig(SpringJUnitConfigTest.Config.class) is equivalent to: + * + * @ExtendWith(SpringExtension.class) + * @ContextConfiguration(classes = SpringJUnitConfigTest.Config.class ) + * + */ +@SpringJUnitConfig(SpringJUnitConfigIntegrationTest.Config.class) +public class SpringJUnitConfigIntegrationTest { + + @Configuration + static class Config { + } + + @Autowired + private ApplicationContext applicationContext; + + @Test + void givenAppContext_WhenInjected_ThenItShouldNotBeNull() { + assertNotNull(applicationContext); + } + +} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitConfigurationParameterizedTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitConfigurationParameterizedTest.java new file mode 100644 index 0000000000..92874e2d59 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitConfigurationParameterizedTest.java @@ -0,0 +1,22 @@ +package com.baeldung.springjunitconfiguration; + +import java.util.List; +import org.junit.jupiter.params.ParameterizedTest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + + +@SpringJUnitConfig(classes = TestConfig.class , loader = AnnotationConfigContextLoader.class) +public class SpringJUnitConfigurationParameterizedTest { + + @ParameterizedTest + @ValueSource(strings = { "Dilbert", "Wally" }) + void people(String name, @Autowired List people) { + assertThat(people.stream() + .map(Person::getName) + .filter(name::equals)).hasSize(1); + } +} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitWebConfigIntegrationTest.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitWebConfigIntegrationTest.java new file mode 100644 index 0000000000..42422f4537 --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/SpringJUnitWebConfigIntegrationTest.java @@ -0,0 +1,34 @@ +package com.baeldung.springjunitconfiguration; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.web.context.WebApplicationContext; + +import static org.junit.Assert.assertNotNull; + +/** + * @SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class) is equivalent to: + * + * @ExtendWith(SpringExtension.class) + * @WebAppConfiguration + * @ContextConfiguration(classes = SpringJUnitWebConfigTest.Config.class ) + * + */ +@SpringJUnitWebConfig(SpringJUnitWebConfigIntegrationTest.Config.class) +public class SpringJUnitWebConfigIntegrationTest { + + @Configuration + static class Config { + } + + @Autowired + private WebApplicationContext webAppContext; + + @Test + void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() { + assertNotNull(webAppContext); + } + +} diff --git a/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/TestConfig.java b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/TestConfig.java new file mode 100644 index 0000000000..a02c542e8d --- /dev/null +++ b/spring-boot-modules/spring-boot-3/src/test/java/com/baeldung/springjunitconfiguration/TestConfig.java @@ -0,0 +1,19 @@ +package com.baeldung.springjunitconfiguration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Primary; +@Configuration +public class TestConfig { + + @Bean + Person dilbert() { + return new Person("Dilbert"); + } + + @Bean + Person wally() { + return new Person("Wally"); + } + +} \ No newline at end of file