mardi 20 octobre 2020

Mockito, Junit Testing of a if condition

I'm trying to write a test for a if condition on my rest service on a delete operation the issue is or I have the mismatch error or get the 404 response, right now I have all this, maybe an overkill, because I have been messing around:

Rest Service :

    public ResponseEntity<?> deleteCommentById(@PathVariable Integer commentId) {
       for (int i = 0; i < commentsList.size(); i++) {
            if (commentsList.get(i).getId() == commentId) {
                commentsList.remove(i);
                commentRepository.save(commentsList.get(i));
                break;
            }
        }
        return new ResponseEntity<>(HttpStatus.OK);

@Test

    @MockBean
    private CommentRepository commentRepository;
    @MockBean
    private List<Comment> commentList;
    @MockBean
    private Comment comment;

    void deleteCommentByIdWithValidId() throws Exception {
        int validId = 888;

--->    when(commentList.get(anyInt())).thenReturn(comment);
        when(comment.getId()).thenReturn(validId);

        mockMvc.perform(delete("/comments/{validId}", validId))
                .andExpect(status().isOk());

    }

I have pointed out where the problem seems to be

Output:

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Misplaced or misused argument matcher detected here:

-> at com.hro.exercise.nbachallenge.controller.rest.RestCommentControllerTest.deleteCommentByIdWithValidId(RestCommentControllerTest.java:139)

Aucun commentaire:

Enregistrer un commentaire