简体   繁体   中英

dynamically load test property file and service property file in spring boot

Writing junit for spring boot service class. My problem is, I am having two properties files(one for application and another for test) During junit i want to load test property file and during application, i want to load application properties file.But always it loads my application service properties file.

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/

And I need to load test-service.properties file during junit and i need to load service.properties during my applicartion run

  1. your test properties file should be located in test folder (in resources)
  2. if your properties file named by application.properties (application-{profile}.properties) and properties file for testing application-test.properties, spring boot load properties hierarchy will be : booting application.properties and then load application-test.properties file, spring overrides values in application properties from application-test properties. ( Spring properties )

if you want to tell spring where it should search properties filed for testing you could use something like that:

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

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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