From 164ef673259867206792299197a1250e66a96649 Mon Sep 17 00:00:00 2001 From: Oscar Mauricio Forero Carrillo Date: Sat, 30 Dec 2023 09:42:21 +0100 Subject: [PATCH] Bael 7317 (#15415) * BAEL-7226: Code for the improvements of the Percentages article. * BAEL-7226: Rename Test to follow the convention * BAEL-7317: Update the code to the latest versions of Spock, Groovy and the maven matching configuration * BAEL-7317: Add code for the article improvements --- testing-modules/groovy-spock/pom.xml | 39 +++++++++++++++---- .../src/main/java/mocks/Book.java | 11 ++++++ .../src/main/java/mocks/BookRepository.java | 5 +++ .../src/main/java/mocks/BookService.java | 13 +++++++ .../src/main/java/mocks/MessageService.java | 7 ++++ .../src/main/java/mocks/ShoppingCart.java | 26 +++++++++++++ .../src/main/java/mocks/UtilityClass.java | 7 ++++ .../groovy/extensions/ShoppingCartTest.groovy | 21 ++++++++++ .../test/groovy/mocks/BookServiceTest.groovy | 25 ++++++++++++ .../groovy/mocks/MessageServiceTest.groovy | 20 ++++++++++ 10 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 testing-modules/groovy-spock/src/main/java/mocks/Book.java create mode 100644 testing-modules/groovy-spock/src/main/java/mocks/BookRepository.java create mode 100644 testing-modules/groovy-spock/src/main/java/mocks/BookService.java create mode 100644 testing-modules/groovy-spock/src/main/java/mocks/MessageService.java create mode 100644 testing-modules/groovy-spock/src/main/java/mocks/ShoppingCart.java create mode 100644 testing-modules/groovy-spock/src/main/java/mocks/UtilityClass.java create mode 100644 testing-modules/groovy-spock/src/test/groovy/extensions/ShoppingCartTest.groovy create mode 100644 testing-modules/groovy-spock/src/test/groovy/mocks/BookServiceTest.groovy create mode 100644 testing-modules/groovy-spock/src/test/groovy/mocks/MessageServiceTest.groovy diff --git a/testing-modules/groovy-spock/pom.xml b/testing-modules/groovy-spock/pom.xml index 7f456399fe..7920713810 100644 --- a/testing-modules/groovy-spock/pom.xml +++ b/testing-modules/groovy-spock/pom.xml @@ -15,6 +15,12 @@ 1.0.0-SNAPSHOT + + 2.4-M1-groovy-4.0 + 4.0.16 + 3.0.2 + + org.spockframework @@ -23,9 +29,10 @@ test - org.codehaus.groovy + org.apache.groovy groovy-all ${groovy-all.version} + pom @@ -39,18 +46,34 @@ compile - testCompile + compileTests + + + + ${project.basedir}/src/test/groovy + + **/*.groovy + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.2.2 + + src/test/groovy + + **/*Specification.groovy + **/*Test.groovy + + - - 1.3-groovy-2.4 - 2.4.7 - 1.5 - - \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/main/java/mocks/Book.java b/testing-modules/groovy-spock/src/main/java/mocks/Book.java new file mode 100644 index 0000000000..b3f3f78606 --- /dev/null +++ b/testing-modules/groovy-spock/src/main/java/mocks/Book.java @@ -0,0 +1,11 @@ +package mocks; + +public class Book { + private String title; + private String author; + + Book(String title, String author) { + this.title = title; + this.author = author; + } +} diff --git a/testing-modules/groovy-spock/src/main/java/mocks/BookRepository.java b/testing-modules/groovy-spock/src/main/java/mocks/BookRepository.java new file mode 100644 index 0000000000..2628a6cab3 --- /dev/null +++ b/testing-modules/groovy-spock/src/main/java/mocks/BookRepository.java @@ -0,0 +1,5 @@ +package mocks; + +public interface BookRepository { + Book findById(Long id); +} \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/main/java/mocks/BookService.java b/testing-modules/groovy-spock/src/main/java/mocks/BookService.java new file mode 100644 index 0000000000..750affec9e --- /dev/null +++ b/testing-modules/groovy-spock/src/main/java/mocks/BookService.java @@ -0,0 +1,13 @@ +package mocks; + +public class BookService { + private final BookRepository repository; + + public BookService(BookRepository repository) { + this.repository = repository; + } + + public Book getBookDetails(Long id) { + return repository.findById(id); + } +} diff --git a/testing-modules/groovy-spock/src/main/java/mocks/MessageService.java b/testing-modules/groovy-spock/src/main/java/mocks/MessageService.java new file mode 100644 index 0000000000..f9e4c5d938 --- /dev/null +++ b/testing-modules/groovy-spock/src/main/java/mocks/MessageService.java @@ -0,0 +1,7 @@ +package mocks; + +public class MessageService { + public String fetchMessage() { + return UtilityClass.getMessage(); + } +} \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/main/java/mocks/ShoppingCart.java b/testing-modules/groovy-spock/src/main/java/mocks/ShoppingCart.java new file mode 100644 index 0000000000..94444c426f --- /dev/null +++ b/testing-modules/groovy-spock/src/main/java/mocks/ShoppingCart.java @@ -0,0 +1,26 @@ +package mocks; + +import java.util.HashMap; +import java.util.Map; + +public class ShoppingCart { + private final Map items = new HashMap<>(); + private double totalPrice = 0.0; + + public void addItem(String item, int quantity) { + items.put(item, items.getOrDefault(item, 0) + quantity); + totalPrice += quantity * 2.00; // Example pricing + } + + public int getTotalItems() { + return items.values().stream().mapToInt(Integer::intValue).sum(); + } + + public double getTotalPrice() { + return totalPrice; + } + + public boolean containsItem(String item) { + return items.containsKey(item); + } +} \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/main/java/mocks/UtilityClass.java b/testing-modules/groovy-spock/src/main/java/mocks/UtilityClass.java new file mode 100644 index 0000000000..a218e9088c --- /dev/null +++ b/testing-modules/groovy-spock/src/main/java/mocks/UtilityClass.java @@ -0,0 +1,7 @@ +package mocks; + +public class UtilityClass { + public static String getMessage() { + return "Original Message"; + } +} \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/test/groovy/extensions/ShoppingCartTest.groovy b/testing-modules/groovy-spock/src/test/groovy/extensions/ShoppingCartTest.groovy new file mode 100644 index 0000000000..116951988d --- /dev/null +++ b/testing-modules/groovy-spock/src/test/groovy/extensions/ShoppingCartTest.groovy @@ -0,0 +1,21 @@ +package extensions + +import mocks.ShoppingCart +import spock.lang.Specification + +class ShoppingCartTest extends Specification { + def "verify multiple properties of a ShoppingCart"() { + given: + ShoppingCart cart = new ShoppingCart() + cart.addItem("Apple", 3) + cart.addItem("Banana", 2) + + expect: + with(cart) { + totalItems == 5 + totalPrice == 10.00 + items.contains("Apple") + items.contains("Banana") + } + } +} \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/test/groovy/mocks/BookServiceTest.groovy b/testing-modules/groovy-spock/src/test/groovy/mocks/BookServiceTest.groovy new file mode 100644 index 0000000000..b33cf6c015 --- /dev/null +++ b/testing-modules/groovy-spock/src/test/groovy/mocks/BookServiceTest.groovy @@ -0,0 +1,25 @@ +package mocks + +import spock.lang.Specification + + +class BookServiceTest extends Specification { + def "should retrieve book details and verify method calls"() { + given: + def bookRepository = Mock(BookRepository) { + findById(1L) >> new Book("Effective Java", "Joshua Bloch") + findById(2L) >> null + } + def bookService = new BookService(bookRepository) + + when: + Book effectiveJava = bookService.getBookDetails(1L) + Book unknownBook = bookService.getBookDetails(2L) + + then: + 1 * bookRepository.findById(1L) + 1 * bookRepository.findById(2L) + effectiveJava.title == "Effective Java" + unknownBook == null + } +} \ No newline at end of file diff --git a/testing-modules/groovy-spock/src/test/groovy/mocks/MessageServiceTest.groovy b/testing-modules/groovy-spock/src/test/groovy/mocks/MessageServiceTest.groovy new file mode 100644 index 0000000000..7eeea90e41 --- /dev/null +++ b/testing-modules/groovy-spock/src/test/groovy/mocks/MessageServiceTest.groovy @@ -0,0 +1,20 @@ +package mocks + +import spock.lang.Specification + + +class MessageServiceTest extends Specification { + def "should use global mock for UtilityClass"() { + given: + def utilityMock = GroovySpy(UtilityClass, global: true) + utilityMock.getMessage() >> "Mocked Message" + + when: + MessageService service = new MessageService() + String message = service.fetchMessage() + + then: + 1 * utilityMock.getMessage() + message == "Mocked Message" + } +} \ No newline at end of file