【发布时间】:2016-08-03 11:01:18
【问题描述】:
我编写了一个 Spring Boot 控制器,用于侦听发送到 /orders/ 的 PUT 请求。
在我的集成测试中,我注意到TestRestTemplate 没有像我预期的那样对 404 响应做出异常反应。这导致像这样的测试通过:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT)
public class OrderControllerTest {
@Autowired
private TestRestTemplate restTemplate;
@Test
public void testValidPut() throws Exception {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>("{}", headers);
restTemplate.put("/doesntexist", entity);
}
}
当我预计 put 方法会抛出异常时,如 the documentation 中所述:
投掷:
RestClientException - 客户端 HTTP 错误
我已确认,如果我正常运行我的应用程序,我会在尝试 PUT 到同一 URL 时收到 404。
所以要么我没有出于某种原因在这种情况下得到 404,要么我误解了 TestRestTemplate 的工作原理。有什么建议吗?
【问题讨论】:
标签: java spring http spring-boot integration-testing