Java 在spring引导中动态加载测试属性文件和服务属性文件
为spring引导服务类编写junit。我的问题是,在junit期间,我有两个属性文件(一个用于应用程序,另一个用于测试),我想加载测试属性文件,在应用程序期间,我想加载应用程序属性文件。但它总是加载我的应用程序服务属性文件 Service.java Test-service.properties 我需要在junit期间加载test-service.properties文件,并且需要在应用程序运行期间加载service.properties文件Java 在spring引导中动态加载测试属性文件和服务属性文件,java,spring,spring-boot,Java,Spring,Spring Boot,为spring引导服务类编写junit。我的问题是,在junit期间,我有两个属性文件(一个用于应用程序,另一个用于测试),我想加载测试属性文件,在应用程序期间,我想加载应用程序属性文件。但它总是加载我的应用程序服务属性文件 Service.java Test-service.properties 我需要在junit期间加载test-service.properties文件,并且需要在应用程序运行期间加载service.properties文件 Use the PropertySourcesPl
Use the PropertySourcesPlaceholderConfigurer in order to mention the property for service and override with test configuration when you run the test through Junit
Something like this
`
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
@Configuration
public class PropertyTestConfiguration {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(ArrayUtils.addAll(
new PathMatchingResourcePatternResolver().getResources("classpath*:application.properties"),
new PathMatchingResourcePatternResolver().getResources("classpath*:test.properties")
)
);
return ppc;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class PropertyTests {
@Value("${elastic.index}")
String index;
@Configuration
@Import({PropertyTestConfiguration.class})
static class ContextConfiguration {
}
}
`
@TestPropertySource({"classpath:/application.properties",classpath:/application-test.properties"})
@ActiveProfiles(profiles = "test")
但在应用程序服务类中,我们提到了propertysource和@value for private变量。我是否需要在服务类中添加一些东西,比如在服务中删除propertysource注释class@karthick如果我的回答对你有帮助,请随意投票)
service.common.software.url=http://192.168.99.100:8080/softwares
service.common.thing.url=http://192.168.99.100:8080/thing
service.common.software.url=http://192.168.99.100:8080/deviceModels
service.common.software.delete.url=http://192.168.99.100:8080/deviceModels/
service.common.software.url=http://localhost:8083/softwares
service.common.thing.url=http://localhost:8083/thing
service.common.software.url=http://localhost:8083/deviceModels
service.common.software.delete.url=http://localhost:8083/deviceModels/
Use the PropertySourcesPlaceholderConfigurer in order to mention the property for service and override with test configuration when you run the test through Junit
Something like this
`
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.IOException;
@Configuration
public class PropertyTestConfiguration {
@Bean
public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() throws IOException {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setLocations(ArrayUtils.addAll(
new PathMatchingResourcePatternResolver().getResources("classpath*:application.properties"),
new PathMatchingResourcePatternResolver().getResources("classpath*:test.properties")
)
);
return ppc;
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class PropertyTests {
@Value("${elastic.index}")
String index;
@Configuration
@Import({PropertyTestConfiguration.class})
static class ContextConfiguration {
}
}
`
@TestPropertySource({"classpath:/application.properties",classpath:/application-test.properties"})
@ActiveProfiles(profiles = "test")