additional answers mockito change repository method (#9082)
This commit is contained in:
parent
7178f731f1
commit
86c4eea3dd
|
@ -1,5 +1,9 @@
|
||||||
package com.baeldung.mockito.additionalanswers;
|
package com.baeldung.mockito.additionalanswers;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
public class BookRepository {
|
public class BookRepository {
|
||||||
public Book getByBookId(Long bookId) {
|
public Book getByBookId(Long bookId) {
|
||||||
return new Book(bookId, "To Kill a Mocking Bird", "Harper Lee", 256);
|
return new Book(bookId, "To Kill a Mocking Bird", "Harper Lee", 256);
|
||||||
|
@ -9,9 +13,12 @@ public class BookRepository {
|
||||||
return new Book(book.getBookId(), book.getTitle(), book.getAuthor(), book.getNumberOfPages());
|
return new Book(book.getBookId(), book.getTitle(), book.getAuthor(), book.getNumberOfPages());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Book checkIfEquals(Book bookOne, Book bookTwo, Book bookThree) {
|
public Book selectRandomBook(Book bookOne, Book bookTwo, Book bookThree) {
|
||||||
if (bookOne.equals(bookTwo) && bookTwo.equals(bookThree) && bookThree.equals(bookOne)) {
|
List<Book> selection = new ArrayList<>();
|
||||||
return bookOne;
|
selection.add(bookOne);
|
||||||
} else return bookTwo;
|
selection.add(bookTwo);
|
||||||
|
selection.add(bookThree);
|
||||||
|
Random random = new Random();
|
||||||
|
return selection.get(random.nextInt(selection.size()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,8 @@ public class BookService {
|
||||||
return bookRepository.save(book);
|
return bookRepository.save(book);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Book checkifEquals(Book book1, Book book2, Book book3) {
|
public Book selectRandomBook(Book book1, Book book2, Book book3) {
|
||||||
return bookRepository.checkIfEquals(book1, book2, book3);
|
return bookRepository.selectRandomBook(book1, book2, book3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -34,9 +34,9 @@ public class BookServiceUnitTest {
|
||||||
Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300);
|
Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300);
|
||||||
Book book3 = new Book(3L, "Romeo and Juliet", "William Shakespeare", 200);
|
Book book3 = new Book(3L, "Romeo and Juliet", "William Shakespeare", 200);
|
||||||
|
|
||||||
Mockito.when(bookRepository.checkIfEquals(any(Book.class), any(Book.class), any(Book.class))).then(AdditionalAnswers.returnsSecondArg());
|
Mockito.when(bookRepository.selectRandomBook(any(Book.class), any(Book.class), any(Book.class))).then(AdditionalAnswers.returnsSecondArg());
|
||||||
|
|
||||||
Book secondBook = bookService.checkifEquals(book1, book2, book3);
|
Book secondBook = bookService.selectRandomBook(book1, book2, book3);
|
||||||
|
|
||||||
assertEquals(secondBook, book2);
|
assertEquals(secondBook, book2);
|
||||||
}
|
}
|
||||||
|
@ -47,9 +47,9 @@ public class BookServiceUnitTest {
|
||||||
Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300);
|
Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300);
|
||||||
Book book3 = new Book(3L, "Romeo and Juliet", "William Shakespeare", 200);
|
Book book3 = new Book(3L, "Romeo and Juliet", "William Shakespeare", 200);
|
||||||
|
|
||||||
Mockito.when(bookRepository.checkIfEquals(any(Book.class), any(Book.class), any(Book.class))).then(AdditionalAnswers.returnsLastArg());
|
Mockito.when(bookRepository.selectRandomBook(any(Book.class), any(Book.class), any(Book.class))).then(AdditionalAnswers.returnsLastArg());
|
||||||
|
|
||||||
Book lastBook = bookService.checkifEquals(book1, book2, book3);
|
Book lastBook = bookService.selectRandomBook(book1, book2, book3);
|
||||||
assertEquals(lastBook, book3);
|
assertEquals(lastBook, book3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,9 +59,9 @@ public class BookServiceUnitTest {
|
||||||
Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300);
|
Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300);
|
||||||
Book book3 = new Book(3L, "Romeo and Juliet", "William Shakespeare", 200);
|
Book book3 = new Book(3L, "Romeo and Juliet", "William Shakespeare", 200);
|
||||||
|
|
||||||
Mockito.when(bookRepository.checkIfEquals(any(Book.class), any(Book.class), any(Book.class))).then(AdditionalAnswers.returnsArgAt(1));
|
Mockito.when(bookRepository.selectRandomBook(any(Book.class), any(Book.class), any(Book.class))).then(AdditionalAnswers.returnsArgAt(1));
|
||||||
|
|
||||||
Book bookOnIndex = bookService.checkifEquals(book1, book2, book3);
|
Book bookOnIndex = bookService.selectRandomBook(book1, book2, book3);
|
||||||
|
|
||||||
assertEquals(bookOnIndex, book2);
|
assertEquals(bookOnIndex, book2);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue