dimanche 11 septembre 2016

Does this unit test cover all edge cases?

I want to test a simple controller and I want to know if this unit test covers all cases:

Controller:

@RequestMapping(method = RequestMethod.GET)
    public List<Tags> getAllTags(@RequestParam(value = "query", required = false) String query,
            @RequestParam(value = "limit", required = false) Integer limit,
            @RequestParam(value = "start", required = false) Integer start,
            @RequestParam(value = "approved", required = false) Boolean approved,
            @ActiveLanguage Language language,
            @ActiveUser User user) {
        if (limit == null) {
            limit = 10;
        }
        if (start == null) {
            start = 0;
        }
        if (approved == null) {
            approved = true;
        }
        if (query != null) {
            return tagService.findTagsByQuery(user, query, limit, start, language, approved);
        } else {
            return tagService.findTags(user, limit, start, language);
        }
    }

Test:

 @Test
    public void testGetAllTags_query_notnull() throws Exception {
        List<Tags> tagsList = createTagList();

        //Individual parameters
        String query = "test";
        Boolean approved = true;

        Mockito.when(this.tagServiceMock.findTagsByQuery(user, query, limit, start, language, approved)).thenReturn(tagsList);

        List<Tags> t = tagController.getAllTags(query, limit, start, approved, language, user);

        assertSame(tagsList, t);
        assertNotNull(t);
        assertNotNull(tagsList);

        verify(tagServiceMock, times(1)).findTagsByQuery(user, query, limit, start, language, approved);
        verifyNoMoreInteractions(tagServiceMock);

    }

    @Test
    public void testGetAllTags_query_null() throws Exception {
        List<Tags> tagsList = createTagList();

        //Individiual Parameters        
        String query = null;
        Boolean approved = true;

        Mockito.when(this.tagServiceMock.findTags(user, limit, start, language)).thenReturn(tagsList);

        List<Tags> t = tagController.getAllTags(query, limit, start, approved, language, user);
        assertEquals(tagsList, t);
        assertNotNull(t);
        assertNotNull(tagsList);

        verify(tagServiceMock, times(1)).findTags(user, limit, start, language);
        verifyNoMoreInteractions(tagServiceMock);

    }

Is there anything left that is not covered by this unit test?

Aucun commentaire:

Enregistrer un commentaire