michaelin007 2023-12-21 09:26:29 +00:00
parent 784cb2335c
commit e8b2ea9a92
5 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package com.baeldung.springjunitconfiguration;
public class Person {
String name;
public Person(String name) {
this.name = name;
}
}

View File

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

View File

@ -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<Person> people) {
assertThat(people.stream()
.map(Person::getName)
.filter(name::equals)).hasSize(1);
}
}

View File

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

View File

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