From 86c4eea3dd95a0e98e6bb882e482fe0702adbd20 Mon Sep 17 00:00:00 2001 From: Belma Jakupovic Date: Wed, 15 Apr 2020 21:15:59 +0200 Subject: [PATCH] additional answers mockito change repository method (#9082) --- .../mockito/additionalanswers/BookRepository.java | 15 +++++++++++---- .../mockito/additionalanswers/BookService.java | 4 ++-- .../additionalanswers/BookServiceUnitTest.java | 12 ++++++------ 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java index 78187e3f01..677ee502b4 100644 --- a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookRepository.java @@ -1,5 +1,9 @@ package com.baeldung.mockito.additionalanswers; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; + public class BookRepository { public Book getByBookId(Long bookId) { 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()); } - public Book checkIfEquals(Book bookOne, Book bookTwo, Book bookThree) { - if (bookOne.equals(bookTwo) && bookTwo.equals(bookThree) && bookThree.equals(bookOne)) { - return bookOne; - } else return bookTwo; + public Book selectRandomBook(Book bookOne, Book bookTwo, Book bookThree) { + List selection = new ArrayList<>(); + selection.add(bookOne); + selection.add(bookTwo); + selection.add(bookThree); + Random random = new Random(); + return selection.get(random.nextInt(selection.size())); } } diff --git a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java index 92c01f8a70..4499a6524b 100644 --- a/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java +++ b/testing-modules/mockito-2/src/main/java/com/baeldung/mockito/additionalanswers/BookService.java @@ -15,8 +15,8 @@ public class BookService { return bookRepository.save(book); } - public Book checkifEquals(Book book1, Book book2, Book book3) { - return bookRepository.checkIfEquals(book1, book2, book3); + public Book selectRandomBook(Book book1, Book book2, Book book3) { + return bookRepository.selectRandomBook(book1, book2, book3); } } diff --git a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java index c9527ec0ec..ee32bcf70c 100644 --- a/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java +++ b/testing-modules/mockito-2/src/test/java/com/baeldung/mockito/additionalanswers/BookServiceUnitTest.java @@ -34,9 +34,9 @@ public class BookServiceUnitTest { Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300); 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); } @@ -47,9 +47,9 @@ public class BookServiceUnitTest { Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300); 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); } @@ -59,9 +59,9 @@ public class BookServiceUnitTest { Book book2 = new Book(2L, "Animal Farm", "George Orwell", 300); 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); }