Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在spring引导中动态加载测试属性文件和服务属性文件_Java_Spring_Spring Boot - Fatal编程技术网

Java 在spring引导中动态加载测试属性文件和服务属性文件

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

为spring引导服务类编写junit。我的问题是,在junit期间,我有两个属性文件(一个用于应用程序,另一个用于测试),我想加载测试属性文件,在应用程序期间,我想加载应用程序属性文件。但它总是加载我的应用程序服务属性文件

Service.java

Test-service.properties

我需要在junit期间加载test-service.properties文件,并且需要在应用程序运行期间加载service.properties文件

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 {
    }
}

`
  • 您的测试属性文件应该位于测试文件夹中(参考资料中)
  • 如果以application.properties(application-{profile}.properties)命名的属性文件和用于测试application-test.properties的属性文件,spring引导加载属性层次结构将是:booting application.properties,然后加载application-test.properties文件,spring从应用程序测试属性中重写应用程序属性中的值。 ()
  • 如果您想告诉spring它应该在哪里搜索用于测试的属性文件,您可以使用以下内容:

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