vendredi 12 juin 2020

Spring Boot Tests- return 200 when should 404

I wrote an application with Warehouses. I have spring functions, i created Exceptions and Controller to them. Problem is when i am trying to test them. I send request as "GET" to get free space of warehouse (actualspace/100 to get %). Id of WH is -5 so i expect to get 404 not found. Instead of that in postman or in chrome i get error 500 and in intelij i get 200. Any help? Test:

 @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)

public class Tests {

@Autowired
private MockMvc mvc;

@Autowired
private WebApplicationContext webApplicationContext;

@MockBean
WareHouseController wareHouseController;

@Before
public void setUp() {
    this.mvc = webAppContextSetup(webApplicationContext).build();
}

FullfilmentContainer is list with warehouses, each warehouse have place,id,nam etc and product list, each product list have items (name, amount etc) and each item have rating list (ratings with date, number)

Tested funcion:

@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 returns a number (its ok). And Controller for Exception:

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);
}

}

and Exception:

public class FulfillmentNotFoundException extends RuntimeException {
private static final long serialVersionUID=1L;

}

Any ideas what i done wrong?

Aucun commentaire:

Enregistrer un commentaire