找不到带有 id:2 的书 - @InjectMocks 不工作 - SpringBoot
Book with id:2 is not found - @InjectMocks is not working - SpringBoot
我正在尝试测试 BookService class 和那个 class 方法 findBookById()
@RunWith(MockitoJUnitRunner.class)
class BookServiceTest {
@InjectMocks
BookService bookService;
@Mock
BookRepository bookRepository;
@Mock
private ModelMapper modelMapper;
private BookDto bookDto = new BookDto();
private Book book = new Book();
private Book book1 = new Book();
private List<Book> books = new ArrayList<>();
private List<BookDto> bookList = new ArrayList<>();
@Test
void findBookById() {
book.setId(2L);
book.setTitle("Harry Potter");
book.setAuthor("J. K. Rowling");
book.setPublisher("Bloomsbury Publishing");
book.setCategory(Category.CLASSIC);
book.setTotalCount(10);
book.setRented(0);
bookDto = bookService.findBookById(2L);
Mockito.when(bookRepository.findById(2L)).thenReturn(java.util.Optional.ofNullable(book));
assertNotNull(bookDto);
assertEquals("Harry Potter", bookDto.getTitle());
assertEquals(book, bookService.findBookById(2L));
}
从这段代码我得到一个错误
com.example.library.exception.BookNotFoundException: Book with id:2 is not found.
更新
当我尝试做 assertEquals(book, bookService.findBookById(2L));
我收到以下错误:
org.opentest4j.AssertionFailedError: Expected :Book(id=2, title=Harry Potter, author=J. K. Rowling, publisher=Bloomsbury Publishing, category=CLASSIC, totalCount=10, rented=0) Actual :BookDto(id=2, title=Harry Potter, author=J. K. Rowling, publisher=Bloomsbury Publishing, category=CLASSIC, totalCount=10, rented=0)
我认为这是由于 bookService
的错误注入造成的。我在这里错过了什么?
您先调用 bookService.findBookById(2L)
,然后存根 bookRepository.findById(2L)
。存根必须先进行,然后再调用被测方法。
Mockito.when(bookRepository.findById(2L)).thenReturn(java.util.Optional.ofNullable(book));
var bookDto = bookService.findBookById(2L);
assertNotNull(bookDto);
assertEquals("Harry Potter", bookDto.getTitle());
我正在尝试测试 BookService class 和那个 class 方法 findBookById()
@RunWith(MockitoJUnitRunner.class)
class BookServiceTest {
@InjectMocks
BookService bookService;
@Mock
BookRepository bookRepository;
@Mock
private ModelMapper modelMapper;
private BookDto bookDto = new BookDto();
private Book book = new Book();
private Book book1 = new Book();
private List<Book> books = new ArrayList<>();
private List<BookDto> bookList = new ArrayList<>();
@Test
void findBookById() {
book.setId(2L);
book.setTitle("Harry Potter");
book.setAuthor("J. K. Rowling");
book.setPublisher("Bloomsbury Publishing");
book.setCategory(Category.CLASSIC);
book.setTotalCount(10);
book.setRented(0);
bookDto = bookService.findBookById(2L);
Mockito.when(bookRepository.findById(2L)).thenReturn(java.util.Optional.ofNullable(book));
assertNotNull(bookDto);
assertEquals("Harry Potter", bookDto.getTitle());
assertEquals(book, bookService.findBookById(2L));
}
从这段代码我得到一个错误
com.example.library.exception.BookNotFoundException: Book with id:2 is not found.
更新
当我尝试做 assertEquals(book, bookService.findBookById(2L));
我收到以下错误:
org.opentest4j.AssertionFailedError: Expected :Book(id=2, title=Harry Potter, author=J. K. Rowling, publisher=Bloomsbury Publishing, category=CLASSIC, totalCount=10, rented=0) Actual :BookDto(id=2, title=Harry Potter, author=J. K. Rowling, publisher=Bloomsbury Publishing, category=CLASSIC, totalCount=10, rented=0)
我认为这是由于 bookService
的错误注入造成的。我在这里错过了什么?
您先调用 bookService.findBookById(2L)
,然后存根 bookRepository.findById(2L)
。存根必须先进行,然后再调用被测方法。
Mockito.when(bookRepository.findById(2L)).thenReturn(java.util.Optional.ofNullable(book));
var bookDto = bookService.findBookById(2L);
assertNotNull(bookDto);
assertEquals("Harry Potter", bookDto.getTitle());