Пишу проект по оценке книг. Падают тесты контроллеров Author, Book, BookRating. Все написано идентично предыдущим контроллерам, но вылетает ошибка Error creating bean. Пример ошибки для BookController:
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.chernoivan.books.rating.service.BookService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Все аннотации проверил, все на своих местах. Не могу понять в чем дело. Ссылка на Git с проектом: https://github.com/chernoivan/books-rating
Код Book
@Getter
@Setter
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Book {
@Id
@GeneratedValue
private UUID id;
private String title;
private Double bookRating;
private Date releaseDate;
private String info;
@Enumerated(EnumType.STRING)
private BookStatus bookStatus;
@CreatedDate
private Instant createdAt;
@LastModifiedDate
private Instant updatedAt;
@OneToMany(mappedBy = "book", cascade = CascadeType.PERSIST)
private List<Assessment> items;
@OneToMany(mappedBy = "book", cascade = CascadeType.PERSIST)
private List<BookGenre> bookGenres;
@ManyToMany(mappedBy = "authorBooks")
private List<Author> authors = new ArrayList<>();
}
Код BookRepository
@Repository
public interface BookRepository extends CrudRepository<Book, UUID> {
}
Код BookService
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
@Autowired
private TranslationService translationService;
public BookReadDTO getBook(UUID id) {
Book book = getFilmRequired(id);
return translationService.toRead(book);
}
public BookReadDTO createBook(BookCreateDTO create) {
Book book = translationService.toEntity(create);
book = bookRepository.save(book);
return translationService.toRead(book);
}
public BookReadDTO patchBook(UUID id, BookPatchDTO patch) {
Book book = getFilmRequired(id);
translationService.patchEntity(patch, book);
book = bookRepository.save(book);
return translationService.toRead(book);
}
public BookReadDTO updateBook(UUID id, BookPutDTO put) {
Book book = getFilmRequired(id);
translationService.updateEntity(put, book);
book = bookRepository.save(book);
return translationService.toRead(book);
}
public void deleteBook(UUID id) {
bookRepository.delete(getFilmRequired(id));
}
public Book getFilmRequired(UUID id) {
return bookRepository.findById(id).orElseThrow(() ->
new EntityNotFoundException(Book.class, id));
}
}
Код BookController
@RestController
@RequestMapping("api/v1/books")
public class BookController {
@Autowired
private BookService bookService;
@GetMapping("/{id}")
public BookReadDTO getBook(@PathVariable UUID id) {
return bookService.getBook(id);
}
@PutMapping("/{id}")
public BookReadDTO putBook(@PathVariable UUID id, @RequestBody BookPutDTO put) {
return bookService.updateBook(id, put);
}
@PostMapping
public BookReadDTO createBook(@RequestBody BookCreateDTO create) {
return bookService.createBook(create);
}
@PatchMapping("/{id}")
public BookReadDTO patchBook(@PathVariable UUID id, @RequestBody BookPatchDTO patch) {
return bookService.patchBook(id, patch);
}
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable UUID id) {
bookService.deleteBook(id);
}
}
Код BookControllerTest
@RunWith(SpringRunner.class)
@WebMvcTest(controllers = BookController.class)
@ActiveProfiles("test")
public class BookControllerTest {
@Autowired
private BookService bookService;
@Autowired
private MockMvc mvc;
@Test
public void testDeleteBook() throws Exception {
UUID id = UUID.randomUUID();
mvc.perform(delete("/api/v1/books/{id}", id.toString())).andExpect(status().isOk());
Mockito.verify(bookService).deleteBook(id);
}
}