2
我寫了一個Spring Boot控制器,用於偵聽發送到/orders/
的PUT請求。爲什麼TestRestTemplate忽略404客戶端錯誤?
在我的集成測試中,我注意到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錯誤
我已經證實,如果我正常運行我的應用程序,我會得到一個404 att要求放置到相同的URL。
因此,無論我是而不是由於某種原因在此場景中獲得404或我誤解了TestRestTemplate
如何工作。有什麼建議麼?
好的,謝謝。聽起來像是一個小文檔錯誤。如果我想詢問我的PUT的結果,我應該改用調用'exchange'而不是'put'嗎? –
是的,如果你關心答案,你應該使用交換的'PUT'請求 –