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

标签 java spring spring-boot

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

Service.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.properties

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/

Test-service.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) 和用于测试的属性文件命名.properties 文件,spring 从应用程序测试属性覆盖应用程序属性中的值。 ( Spring properties )

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

@TestPropertySource({"classpath:/application.properties",classpath:/application-test.properties"})
@ActiveProfiles(profiles = "test")

关于java - 在spring boot中动态加载测试属性文件和服务属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47976156/

相关文章:

java - Spring 配置文件配置

java - springSecurityFilterChain - ObjectPostProcessor 是必需的 bean 异常

java - Tomcat 上的 SpringBoot : Error creating bean with name 'jacksonObjectMapperBuilder'

java - 作业给出了意想不到的答案

java - Kotlin 内联函数和 Android 方法计数

java - 在 XMLRPC 结果中使用 HashMap

java - Spring Boot应用程序在没有路径的情况下将调用哪个方法

java - Springboot在实现Web安全和mvc配置后不会加载CSS文件夹

spring - 如何在Spring boot中使用注释来使用参数解析器?

java - 无法解析类型 [org.glassfish.jersey.message.filtering.spi.ObjectProvider<com.fasterxml.jackson.databind.ser.FilterProvider>] 的任何 bean