【发布时间】:2020-10-02 17:09:02
【问题描述】:
我使用 Warehouses 编写了一个应用程序。我有弹簧功能,我为它们创建了异常和控制器。问题是当我试图测试它们时。我以“GET”的形式发送请求以获取仓库的可用空间(实际空间/100 以获取 %)。 WH 的 ID 是 -5,所以我希望找不到 404。而不是在邮递员或 chrome 中我得到错误 500 而在 intelij 我得到 200。有什么帮助吗? 测试:
@Test
public void getFillNotExistingTest() throws Exception{
mvc.perform(MockMvcRequestBuilders
.get("/api/fulfillment/-5/fill"))
.andExpect(status().isNotFound());
}
课间休息测试:
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc @WebMvcTest(controllers = WareHouseController.class)
公共类测试{
@Autowired
private MockMvc mvc;
@Autowired
private WebApplicationContext webApplicationContext;
@MockBean
WareHouseController wareHouseController;
@Before
public void setUp() {
this.mvc = webAppContextSetup(webApplicationContext).build();
}
FullfilmentContainer 是仓库列表,每个仓库都有地点、ID、名称等和产品列表,每个产品列表都有项目(名称、数量等),每个项目都有评级列表(评级与日期、编号)
测试功能:
@GetMapping("/api/fulfillment/{wh_id}/fill") //LP9
public ResponseEntity<Object> getPercent(@PathVariable ("wh_id") int wh_id) throws FulfillmentNotFoundException {
FulfillmentCenter ful=FulfillmentCenterContainer.searchID(wh_id);
assert ful != null;
if (ful.getPercent(ful) >= 0)
return new ResponseEntity<>(ful.getPercent(ful), HttpStatus.OK);
else
throw new FulfillmentCenterNotFoundController();
}
函数 getPercent 返回一个数字(没问题)。 和异常控制器:
public class FulfillmentCenterNotFoundController extends RuntimeException {
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(value = FulfillmentNotFoundException.class)
public static ResponseEntity<Object> NotFoundExceptionWH(){
return new ResponseEntity<>("Fulfillment not found- error 404", HttpStatus.NOT_FOUND);
}
}
和例外:
public class FulfillmentNotFoundException extends RuntimeException {
private static final long serialVersionUID=1L;
}
任何想法我做错了什么?
【问题讨论】:
-
如果您收到 500 作为响应,应该有堆栈跟踪吗?
-
我只在邮递员或浏览器中得到这个:/
标签: java spring-boot testing http-error