为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
答案 0 :(得分:1)
如果你想告诉spring它应该搜索提交测试的属性你可以使用类似的东西:
@TestPropertySource({"classpath:/application.properties",classpath:/application-test.properties"})
@ActiveProfiles(profiles = "test")
答案 1 :(得分:0)
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 {
}
}
`