dexter
dexter

Reputation: 285

Error in MockMvc test case for controller

I have written a jUnit test case for controller layer method, and it is failing due to mismatch in expected outcome. Test case is as follows:

 @Test
    public void testGetNodeStatusCount() throws Exception {

        ListNodes listNodes = new ListNodes();
       // ArgumentCaptor<Integer> userId = ArgumentCaptor.forClass(Integer.class);
        when(userManagementHelper.getNodeStatusCount(0)).thenReturn(
            new ResponseEntity<ListNodes>(listNodes, new HttpHeaders(),
                HttpStatus.OK));

        mockMvc.perform(get("/usermgmt/nodestatus")).andExpect(status().isOk());

    }

Method for which this test case is written is as follows :

@RequestMapping(value = "/nodestatus", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<ListNodes> getNodeStatusCount(@RequestParam("userId") int userId) {
        return userManagementHepler.getNodeStatusCount(userId);
    } 

Failure message is : enter image description here

Thing that I am not getting is that if status has been set OK than how I am getting 400 in place of 200.

One more thing I am new to this MockMVC testing for "Controller", so please suggest me a source to learn this.

Upvotes: 1

Views: 1403

Answers (1)

Leon
Leon

Reputation: 12481

You controller expects a required request parameter, which is why you get status 400 (Bad Request)

You can modify your test to include this request parameter mockMvc.perform(get("/usermgmt/nodestatus?userId=0")).andExpect(status().isOk());

or you can make the request parameter optional @RequestParam(value = "userId", required = false, defaultValue = "0")

Upvotes: 1

Related Questions