[英]Not able to read application properties from Spring Boot in JUnit?
I have a JUnit
test that I would like to run against a REST
service which is already running on my machine at the specified port.我有一个JUnit
测试,我想针对REST
服务运行该测试,该服务已在我的机器上的指定端口上运行。 Already tested the REST
service with Postman
and it works fine.已经用Postman
测试了REST
服务,它工作正常。
The plan here is to make the REST
URLs configurable by externalizing them in a properties file.这里的计划是通过在属性文件中将REST
URL 外部化来使它们可配置。 Hence I tried following and the JUnit
class is not able to read the properties file value.因此,我尝试了以下操作,但JUnit
类无法读取属性文件值。
StoreClientTest.java商店客户端测试.java
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource("classpath:application.properties")
public class StoreClientTest {
private static final String STORE_URI = "http://localhost:8084/hpdas/store";
private RestTemplate restTemplate;
@Value("${name}")
private String name;
@Before
public void setUp() {
restTemplate = new RestTemplate();
}
@Test
public void testStore_NotNull() {
//PRINTS ${name} instead of abc ?????
System.out.println("name = " + name);
assertNotNull(restTemplate.getForObject(STORE_URI, Map.class));
}
@After
public void tearDown() {
restTemplate = null;
}
}
src\\test\\main\\resources\\application.properties src\\test\\main\\resources\\application.properties
name=abc
@SpringBootApplication
@PropertySource(value = "classpath:application.properties")
public class TestContextConfiguration {
}
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestContextConfiguration.class)
public class StoreClientTest {
@Value("${name}")
private String name;
// test cases ..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.