繁体   English   中英

spring boot中动态加载测试属性文件和服务属性文件

[英]dynamically load test property file and service property file in spring boot

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

服务.java

@Component
@PropertySource("classpath:service.properties")
public class webModelService implements IWebModelService<webModel> {

    @Value("${service.common.software.url}")
    private String softwareEndPoint;

    @Value("${service.common.software.url}")
    private String createwebEndpoint;

    @Value("${service.common.software.delete.url}")
    private String deletewebEndpoint;

    @Value("${service.common.thing.url}")
    private String createthingEndPoint;


    @Override
    public void save(WebModel wModel)     {

        log.info("Save web model -> start");
        System.out.println("softwareEndPoint===>"+softwareEndPoint);
        System.out.println("createwebEndpoint===>"+createwebEndpoint);
        System.out.println("deletewebEndpoint===>"+deletewebEndpoint);
        System.out.println("createthingEndPoint===>"+createthingEndPoint);
    }


}

Junit.java

@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.ericsson.tmo.iotep.dataimport")
@TestPropertySource("classpath:Test-service.properties")
@ContextConfiguration(classes = { BeansForDefaultValueGenerator.class }, loader = AnnotationConfigContextLoader.class)
public class webModelServiceTest {

    @Autowired
    webModelService webService;

    @Test
    public void testwebModelService(){
        nwebModel.setNotes("Test_notes");

        List<Software> softwareList = new ArrayList<>();
        software.setSoftwareName("Test_software");
        softwareList.add(software);
        anwebModel.setSoftware(softwareList);

        webService.save(anwebModel);
    }

}

服务属性

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/

测试服务.properties

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/

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

  1. 您的测试属性文件应位于测试文件夹中(在资源中)
  2. 如果您的属性文件由 application.properties (application-{profile}.properties) 和用于测试 application-test.properties 的属性文件命名,则 Spring Boot 加载属性层次结构将是:启动 application.properties 然后加载 application-test.properties 文件, spring 覆盖 application-test 属性中的 application 属性中的值。 弹簧特性

如果你想告诉 spring 它应该在哪里搜索用于测试的属性,你可以使用类似的东西:

@TestPropertySource({"classpath:/application.properties",classpath:/application-test.properties"})
@ActiveProfiles(profiles = "test")
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 {
    }
}

`

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM