Autowiring ServerConfig in @Bean method test class
This commit is contained in:
parent
057cab32fc
commit
2b44a323eb
@ -7,7 +7,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
@Configuration
|
@Configuration
|
||||||
public class ServerConfigFactory {
|
public class ServerConfigFactory {
|
||||||
|
|
||||||
@Bean
|
@Bean(name = "default_bean")
|
||||||
@ConfigurationProperties(prefix = "server.default")
|
@ConfigurationProperties(prefix = "server.default")
|
||||||
public ServerConfig getDefaultConfigs() {
|
public ServerConfig getDefaultConfigs() {
|
||||||
return new ServerConfig();
|
return new ServerConfig();
|
||||||
|
@ -8,6 +8,7 @@ import java.util.Map;
|
|||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.TestPropertySource;
|
import org.springframework.test.context.TestPropertySource;
|
||||||
@ -20,17 +21,16 @@ import org.springframework.test.context.junit.jupiter.SpringExtension;
|
|||||||
public class BindingPropertiesToBeanMethodsUnitTest {
|
public class BindingPropertiesToBeanMethodsUnitTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ServerConfigFactory configFactory;
|
@Qualifier("default_bean")
|
||||||
|
private ServerConfig serverConfig;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() {
|
void givenBeanAnnotatedMethod_whenBindingProperties_thenAllFieldsAreSet() {
|
||||||
assertEquals("192.168.0.2", configFactory.getDefaultConfigs()
|
assertEquals("192.168.0.2", serverConfig.getAddress()
|
||||||
.getAddress()
|
|
||||||
.getIp());
|
.getIp());
|
||||||
|
|
||||||
Map<String, String> expectedResourcesPath = new HashMap<>();
|
Map<String, String> expectedResourcesPath = new HashMap<>();
|
||||||
expectedResourcesPath.put("imgs", "/root/def/imgs");
|
expectedResourcesPath.put("imgs", "/root/def/imgs");
|
||||||
assertEquals(expectedResourcesPath, configFactory.getDefaultConfigs()
|
assertEquals(expectedResourcesPath, serverConfig.getResourcesPath());
|
||||||
.getResourcesPath());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user