我用Warehouse编写了一个应用程序。我有spring函数,我为它们创建了异常和控制器。问题是当我尝试测试它们时。我将请求发送为“ GET”以获取仓库的可用空间(actualspace / 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());
}
Rest in class test:
@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,nam等以及产品列表,每个产品列表都有项目(名称,数量等),每个项目都有等级列表(带有日期,编号的等级)
已测试的功能:
@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();
}
Funcion 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;
}
任何想法我做错了什么?
您需要嘲笑FulfillmentCenterContainer.searchID(wh_id)
的通话在下面尝试使用
FulfillmentCenter ful = new FulfillmentCenter();
(ful.settPercent(-5)
Mockito.when(FulfillmentCenterContainer.searchID(Mockito.eq(-5)).thenReturn(ful);