Spring Boot Service JUnit测试Mockito AssertionFailedError from Service(空)

dfty9e19  于 2022-10-04  发布在  Spring
关注(0)|答案(1)|浏览(189)

我有一个问题,那就是在Service JUnit测试中获取服务的结果。

下面是listBooks方法的参数,如下所示。

listBooks(int size, int page, Long userId)

在我实现了列出所有图书的过程后,bookListService.listBooks(2,1,1L)的结果为空。

以下是测试方法的结果,如下所示。

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

测试方法中的问题在哪里?

我怎么才能修好它?

下面是如下所示的测试类。

@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());
    }

}

下面是如下所示的bookListResponseResult。它的尺寸是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)
jobtbby3

jobtbby31#

在我删除所有这些后,如下所示,这个问题消失了。

// 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);

相关问题