Nit*_*tal 4 java rest junit spring spring-boot
我有一个JUnit测试,我想针对REST已在我的机器上指定端口运行的服务运行该测试。已经测试了该REST服务Postman并且它工作正常。
这里的计划是REST通过在属性文件中将URL 外部化来使URL 可配置。因此,我尝试了以下操作,但JUnit该类无法读取属性文件值。
商店客户端测试.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;
}
}
Run Code Online (Sandbox Code Playgroud)
src\test\main\resources\application.properties
name=abc
Run Code Online (Sandbox Code Playgroud)
@SpringBootApplication
@PropertySource(value = "classpath:application.properties")
public class TestContextConfiguration {
}
Run Code Online (Sandbox Code Playgroud)
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TestContextConfiguration.class)
public class StoreClientTest {
@Value("${name}")
private String name;
// test cases ..
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7853 次 |
| 最近记录: |