From 2b44a323ebcfd356a218accb5fbceab850dc1de5 Mon Sep 17 00:00:00 2001 From: "m.raheem" Date: Thu, 27 Feb 2020 16:59:16 +0200 Subject: [PATCH] Autowiring ServerConfig in @Bean method test class --- .../configurationproperties/ServerConfigFactory.java | 2 +- .../BindingPropertiesToBeanMethodsUnitTest.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java index 9d337123c7..ca85d59112 100644 --- a/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java +++ b/spring-boot-modules/spring-boot-testing/src/main/java/com/baeldung/boot/configurationproperties/ServerConfigFactory.java @@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration; @Configuration public class ServerConfigFactory { - @Bean + @Bean(name = "default_bean") @ConfigurationProperties(prefix = "server.default") public ServerConfig getDefaultConfigs() { return new ServerConfig(); diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java index 743cb26a18..82c2a55c32 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/boot/configurationproperties/BindingPropertiesToBeanMethodsUnitTest.java @@ -8,6 +8,7 @@ import java.util.Map; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; @@ -20,17 +21,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; public class BindingPropertiesToBeanMethodsUnitTest { @Autowired - private ServerConfigFactory configFactory; + @Qualifier("default_bean") + private ServerConfig serverConfig; @Test void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() { - assertEquals("192.168.0.2", configFactory.getDefaultConfigs() - .getAddress() + assertEquals("192.168.0.2", serverConfig.getAddress() .getIp()); Map expectedResourcesPath = new HashMap<>(); expectedResourcesPath.put("imgs", "/root/def/imgs"); - assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs() - .getResourcesPath()); + assertEquals(expectedResourcesPath, serverConfig.getResourcesPath()); } } \ No newline at end of file