vendredi 8 juin 2018

Testing the method that has stream () and filter ()

For the method

    public List<OtherTitle> getTitles(
        @Min(1) final Long id
) throws ResourceNotFoundException {
    log.info("Called with id {}", id);

    return this.findMovie(id).getOtherTitles()
            .stream()
            .filter(title -> title.getStatus() == DataStatus.ACCEPTED)
            .map(ServiceUtils::toOtherTitleDto)
            .collect(Collectors.toList());
}

I wrote a test

    @Test
public void canGetMovieTitles() throws ResourceException {
    final Long id = new Random().nextLong();
    final MovieEntity entity = Mockito.mock(MovieEntity.class);
    Mockito.when(entity.getOtherTitles()).thenReturn(Lists.newArrayList());
    Mockito
            .when(this.movieRepository.findByIdAndStatus(id, DataStatus.ACCEPTED))
            .thenReturn(Optional.of(entity));
    Assert.assertTrue(this.service.getTitles(id).isEmpty());
}

With the help of the Jacoco tool, I wanted to check the scope of the tests. It turns out that the getTitles() method is 100% tested, but lambda with this method has a range of 0% https://zapodaj.net/303603912f57f.png.html. In the preview https://zapodaj.net/d68d7252b1d17.png.html, the filter () is highlighted on the yellow.

How do you test this filter() method with stream()?

Aucun commentaire:

Enregistrer un commentaire