2 years ago
#17991
HarryPotter
Java how to test aspect Method
I would like to write tests for my aspects, but I have no idea how to do this or even how to start. I have my service method that saves some object and my point cut aspect that saves information about this operation to my database. I would like to write a test to just see whether my aspect is being executed after calling the service method, but I don't know how I should inject my dependencies or mock them.
My service method:
private final BookRepository bookRepository;
public Book saveBook(final Book book){
return bookRepository.save(book);
}
My aspect:
private final BooksAudRepository booksAudRepository;
private final BookRepository bookRepository;
@After("execution(* com.library.service.BookService.saveBook(..))" +
"&& args(book)")
public void saveBookLogDb(Book book) {
log.info("BookAud INSERT operation is being caught");
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
ReaderDetails readerDetails = (ReaderDetails) authentication.getPrincipal();
BooksAud booksAud = buildSaveBookAud(book, readerDetails.getUsername());
booksAudRepository.save(booksAud);
log.info("BookAud INSERT operation is being recorded");
}
And here I have my test method that is a mix of my attempts to write that test, so there is nothing useful. Can anyone point me out how to start this? How do I call the dependencies?
java
spring
spring-aop
aspect
0 Answers
Your Answer