Search code examples
javaspring-bootjunitmockito

Spring Boot Service JUnit Test Mockito AssertionFailedError from Service (null)


I have a problem about getting the result from service in Service JUnit Test.

Here are the parameters of listBooks method shown below.

listBooks(int size, int page, Long userId)

After I implemented the process of listing all books ffrom service, the result of bookListService.listBooks(2,1,1L) is null.

Here is the result of test method shown below.

org.opentest4j.AssertionFailedError: 
Expected :1
Actual   :null

Where is the issue in the test method?

How can I fix it?

Here is the test class shown below.

@ExtendWith(MockitoExtension.class)
public class BookListServiceTest {

    @InjectMocks
    private BookListService bookListService;

    @Mock
    private BookRepository bookRepository;

    @Mock
    private CategoryService categoryService;

    @Test
    void givenBookList_whenGetAllBooks_thenReturnBookList() {

        // given - precondition or setup
        BookResponse bookResponse1 = BookResponse.builder().bookStatus(BookStatus.READING)
                .title("Book Title 1")
                .categoryId(1L)
                .build();

        BookResponse bookResponse2 = BookResponse.builder().bookStatus(BookStatus.READING)
                .title("Book Title 2")
                .categoryId(1L)
                .build();

        List<BookResponse> bookListResponse = Arrays.asList(bookResponse1,bookResponse2);

        Category category = Category.builder().name(CategoryType.COMIC.getValue()).build();

        SaveBookRequest saveBookRequest1 = SaveBookRequest.builder().bookStatus(BookStatus.READING)
                .title("Book Title 1")
                .userId(1L)
                .categoryId(1L)
                .build();

        SaveBookRequest saveBookRequest2 = SaveBookRequest.builder().bookStatus(BookStatus.READING)
                .title("Book Title 2")
                .userId(1L)
                .categoryId(1L)
                .build();

        Book book1 = Book.builder().category(category)
                .bookStatus(saveBookRequest1.getBookStatus())
                .title(saveBookRequest1.getTitle())
                .publisher(saveBookRequest1.getPublisher())
                .lastPageNumber(saveBookRequest1.getLastPageNumber())
                .authorName(saveBookRequest1.getAuthorName())
                .totalPage(saveBookRequest1.getTotalPage())
                .userId(saveBookRequest1.getUserId())
                .build();

        Book book2 = Book.builder().category(category)
                .bookStatus(saveBookRequest2.getBookStatus())
                .title(saveBookRequest2.getTitle())
                .publisher(saveBookRequest2.getPublisher())
                .lastPageNumber(saveBookRequest2.getLastPageNumber())
                .authorName(saveBookRequest2.getAuthorName())
                .totalPage(saveBookRequest2.getTotalPage())
                .userId(saveBookRequest2.getUserId())
                .build();

        // when -  action or the behaviour that we are going test
        when(categoryService.loadCategory(anyLong())).thenReturn(category);
        when(bookRepository.save(any(Book.class))).thenReturn(book1);
        when(bookRepository.save(any(Book.class))).thenReturn(book2);
        when(
                bookRepository.findAll(any(Specification.class), any(Pageable.class))
        ).thenReturn(new PageImpl<Book>(Arrays.asList(book1,book2)));

        // then - verify the output
        List<BookResponse> bookListResponseResult = bookListService.listBooks(2,1,1L); // Return Null result
        assertEquals(bookListResponse.get(0).getCategoryId(), bookListResponseResult.get(0).getCategoryId());
        assertEquals(bookListResponse.get(0).getTitle(), bookListResponseResult.get(0).getTitle());
        assertEquals(bookListResponse.get(1).getCategoryId(), bookListResponseResult.get(1).getCategoryId());
        assertEquals(bookListResponse.get(1).getTitle(), bookListResponseResult.get(1).getTitle());
    }

}

Here is the bookListResponseResult shown below. It's size is 2.

BookResponse(id=null, title=Book Title 1, authorName=null, bookStatus=null, publisher=null, lastPageNumber=null, image=null, categoryId=null, totalPage=null, imageUrl=null)

BookResponse(id=null, title=Book Title 2, authorName=null, bookStatus=null, publisher=null, lastPageNumber=null, image=null, categoryId=null, totalPage=null, imageUrl=null)

Solution

  • After I removed all these when shown below, the issue disappeared.

    // when -  action or the behaviour that we are going test
    when(categoryService.loadCategory(anyLong())).thenReturn(category);
    when(bookRepository.save(any(Book.class))).thenReturn(book1);
    when(bookRepository.save(any(Book.class))).thenReturn(book2);