personalweb

archived
git clone git://git.wimdupont.com/personalweb.git
Log | Files | Refs | LICENSE

commit 3f3cd143229173efe545c850a4dd498ceb53ac27
parent 3e62f3a001a3827ddd2fb7cbbce715933cb718ec
Author: Wim Dupont <wim@wimdupont.com>
Date:   Sat,  8 Jul 2023 16:36:20 +0200

test improvements


Former-commit-id: df5c051a1a5248a3d245371a74dd7b4af65dd9c7
Diffstat:
Msrc/test/java/com/wimdupont/personalweb/service/BookServiceTest.java | 23+++++++++++++++++++++--
Msrc/test/java/com/wimdupont/personalweb/service/GuideServiceTest.java | 19++++++++++++++-----
2 files changed, 35 insertions(+), 7 deletions(-)

diff --git a/src/test/java/com/wimdupont/personalweb/service/BookServiceTest.java b/src/test/java/com/wimdupont/personalweb/service/BookServiceTest.java @@ -10,11 +10,14 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Direction; import java.util.List; @@ -29,6 +32,8 @@ class BookServiceTest { private BookToDtoConverter bookToDtoConverter; @InjectMocks private BookService bookService; + @Captor + private ArgumentCaptor<Sort> sortArgumentCaptor; @BeforeEach @@ -42,7 +47,7 @@ class BookServiceTest { @Test public void findAllSortedByCategory() { - Mockito.when(bookRepository.findAll(Mockito.any(Sort.class))) + Mockito.when(bookRepository.findAll(sortArgumentCaptor.capture())) .thenReturn(bookList); Mockito.when(bookToDtoConverter.convert(Mockito.any(Book.class))) .thenReturn(BookDto.Builder.newBuilder() @@ -59,7 +64,21 @@ class BookServiceTest { .build()); var result = bookService.findAllSortedByCategory(); - Mockito.verify(bookToDtoConverter, Mockito.times(3)).convert(Mockito.any(Book.class)); + + Mockito.verify(bookToDtoConverter, Mockito.times(3)) + .convert(Mockito.any(Book.class)); + var sortList = sortArgumentCaptor.getValue().get().toList(); + Assertions.assertEquals(5, sortList.size()); + Assertions.assertEquals("category", sortList.get(0).getProperty()); + Assertions.assertEquals(Direction.ASC, sortList.get(0).getDirection()); + Assertions.assertEquals("author", sortList.get(1).getProperty()); + Assertions.assertEquals(Direction.ASC, sortList.get(1).getDirection()); + Assertions.assertEquals("series", sortList.get(2).getProperty()); + Assertions.assertEquals(Direction.ASC, sortList.get(2).getDirection()); + Assertions.assertEquals("seriesNumber", sortList.get(3).getProperty()); + Assertions.assertEquals(Direction.ASC, sortList.get(3).getDirection()); + Assertions.assertEquals("title", sortList.get(4).getProperty()); + Assertions.assertEquals(Direction.ASC, sortList.get(4).getDirection()); Assertions.assertEquals(2, result.size()); Assertions.assertEquals(result.get("Fun").size(), 2); Assertions.assertEquals(result.get("Serious").size(), 1); diff --git a/src/test/java/com/wimdupont/personalweb/service/GuideServiceTest.java b/src/test/java/com/wimdupont/personalweb/service/GuideServiceTest.java @@ -10,6 +10,8 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; @@ -29,6 +31,8 @@ class GuideServiceTest { private GitlabApi gitlabApi; @InjectMocks private GuideService guideService; + @Captor + private ArgumentCaptor<String> sha256Captor; @BeforeEach public void setup() { @@ -58,29 +62,34 @@ class GuideServiceTest { guideService.removeNotFound(List.of(repositoryFile)); - Mockito.verify(guideRepository, Mockito.times(1)).delete(Mockito.any(Guide.class)); + Mockito.verify(guideRepository, Mockito.times(1)) + .delete(Mockito.any(Guide.class)); } @Test public void findAllToUpsertWhenHashFound() { var guideToUpsert = GuideMother.withDefaults().contentSha256("contentSha256").build(); - Mockito.when(guideRepository.findByContentSha256(Mockito.any(String.class))) + Mockito.when(guideRepository.findByContentSha256(sha256Captor.capture())) .thenReturn(Optional.of(guideToUpsert)); var result = guideService.findAllToUpsert(List.of(repositoryFile)); - Mockito.verify(guideRepository, Mockito.times(1)).findByContentSha256(Mockito.any(String.class)); + Mockito.verify(guideRepository, Mockito.times(1)) + .findByContentSha256(Mockito.any(String.class)); + Assertions.assertEquals(repositoryFile.contentSha256(), sha256Captor.getValue()); Assertions.assertEquals(0, result.size()); } @Test public void findAllToUpsertWhenHashNotFound() { - Mockito.when(guideRepository.findByContentSha256(Mockito.any(String.class))) + Mockito.when(guideRepository.findByContentSha256(sha256Captor.capture())) .thenReturn(Optional.empty()); var result = guideService.findAllToUpsert(List.of(repositoryFile)); - Mockito.verify(guideRepository, Mockito.times(1)).findByContentSha256(Mockito.any(String.class)); + Mockito.verify(guideRepository, Mockito.times(1)) + .findByContentSha256(Mockito.any(String.class)); + Assertions.assertEquals(repositoryFile.contentSha256(), sha256Captor.getValue()); Assertions.assertEquals(1, result.size()); Assertions.assertEquals(repositoryFile.path(), result.get(0).getPath()); Assertions.assertEquals(repositoryFile.contentSha256(), result.get(0).getContentSha256());