我有一个JUnit
测试,我想针对REST
已经在我的计算机上指定端口运行的服务运行。已经使用对该REST
服务进行了测试,Postman
并且效果很好。
这里的计划是通过将REST
URL外部化到属性文件中来使URL可配置。因此,我尝试了以下操作,JUnit
该类无法读取属性文件的值。
StoreClientTest.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
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 ..
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句