【发布时间】:2021-01-28 09:51:46
【问题描述】:
我想为我的 BookService 编写一个测试。这就是那个测试。我不知道为什么我总是收到以下错误:
org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter
[com.mrfisherman.library.service.domain.BookService bookService] in constructor
[public com.mrfisherman.library.service.domain.BookServiceTest(com.mrfisherman.library.service.domain.BookService,
com.mrfisherman.library.persistence.repository.BookRepository)].
如您所见,我在这里不使用参数化测试。提前谢谢!
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Server.class)
class BookServiceTest {
private final BookService bookService;
private final BookRepository bookRepository;
public BookServiceTest(BookService bookService, BookRepository bookRepository) {
this.bookService = bookService;
this.bookRepository = bookRepository;
}
@Test
void saveBook() {
//given
Book book = new Book();
book.setTitle("Book 1");
book.setPublishYear(1990);
book.setType(BookFormat.REAL);
book.setIsbn("1234567890");
book.setDescription("Very good book");
book.setNumberOfPages(190);
book.setSummary("Very short summary");
book.setCategories(Set.of(new Category("horror"), new Category("drama")));
//when
bookService.saveBook(book);
//then
Optional<Book> loaded = bookRepository.findById(book.getId());
assertThat(loaded).isPresent();
}
}
【问题讨论】:
标签: java junit junit5 spring-test