Merge pull request #15465 from Michaelin007/springconfig

https://jira.baeldung.com/browse/BAEL-7376
This commit is contained in:
Maiklins 2023-12-28 21:55:22 +01:00 committed by GitHub
commit 3eb54511ed
5 changed files with 73 additions and 16 deletions

View File

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

View File

@ -1,6 +1,4 @@
package com.baeldung.jupiter;
import static org.junit.Assert.assertNotNull;
package com.baeldung.springjunitconfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@ -8,9 +6,11 @@ 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 )
*
@ -18,10 +18,6 @@ import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig(SpringJUnitConfigIntegrationTest.Config.class)
public class SpringJUnitConfigIntegrationTest {
@Configuration
static class Config {
}
@Autowired
private ApplicationContext applicationContext;
@ -30,4 +26,8 @@ public class SpringJUnitConfigIntegrationTest {
assertNotNull(applicationContext);
}
@Configuration
static class Config {
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.springjunitconfiguration;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
@SpringJUnitConfig(classes = TestConfig.class, loader = AnnotationConfigContextLoader.class)
public class SpringJUnitConfigurationUnitTest {
@ParameterizedTest
@ValueSource(strings = { "Dilbert", "Wally" })
void givenPeopleList_whenSetPeopleWithName_thenListContainsOnePerson(String name, @Autowired List<Person> people) {
assertThat(people.stream()
.map(Person::getName)
.filter(name::equals)).hasSize(1);
}
}

View File

@ -1,6 +1,4 @@
package com.baeldung.jupiter;
import static org.junit.Assert.assertNotNull;
package com.baeldung.springjunitconfiguration;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
@ -8,9 +6,11 @@ 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 )
@ -19,10 +19,6 @@ import org.springframework.web.context.WebApplicationContext;
@SpringJUnitWebConfig(SpringJUnitWebConfigIntegrationTest.Config.class)
public class SpringJUnitWebConfigIntegrationTest {
@Configuration
static class Config {
}
@Autowired
private WebApplicationContext webAppContext;
@ -31,4 +27,8 @@ public class SpringJUnitWebConfigIntegrationTest {
assertNotNull(webAppContext);
}
@Configuration
static class Config {
}
}

View File

@ -0,0 +1,19 @@
package com.baeldung.springjunitconfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class TestConfig {
@Bean
Person dilbert() {
return new Person("Dilbert");
}
@Bean
Person wally() {
return new Person("Wally");
}
}