From 5e49dfa4e7dce8e9e3895f04ca556a8a9e08cca5 Mon Sep 17 00:00:00 2001 From: Peter Kofler Date: Tue, 13 Feb 2024 14:19:42 +0100 Subject: [PATCH] Extract and Override Factory Method to control new UI. --- .../org/codecop/dependencies/e/Checkout.java | 8 +++++-- .../codecop/dependencies/e/CheckoutTest.java | 24 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/Java/src/main/java/org/codecop/dependencies/e/Checkout.java b/Java/src/main/java/org/codecop/dependencies/e/Checkout.java index 1c4debd..f842c31 100644 --- a/Java/src/main/java/org/codecop/dependencies/e/Checkout.java +++ b/Java/src/main/java/org/codecop/dependencies/e/Checkout.java @@ -10,11 +10,15 @@ public class Checkout { public Checkout(Product product, EmailService emailService) { this.product = product; this.emailService = emailService; - this.newsLetterSubscribed = new UserConfirmation("Subscribe to our product " + product.name() + " newsletter?"); - this.termsAndConditionsAccepted = new UserConfirmation("Accept our terms and conditions?\n" + + this.newsLetterSubscribed = createUserConfirmation("Subscribe to our product " + product.name() + " newsletter?"); + this.termsAndConditionsAccepted = createUserConfirmation("Accept our terms and conditions?\n" + "(Mandatory to place order for " + product.name() + ")"); } + protected UserConfirmation createUserConfirmation(String message) { + return new UserConfirmation(message); + } + public void confirmOrder() { if (!termsAndConditionsAccepted.isAccepted()) { throw new OrderCancelledException(product); diff --git a/Java/src/test/java/org/codecop/dependencies/e/CheckoutTest.java b/Java/src/test/java/org/codecop/dependencies/e/CheckoutTest.java index 76b5fef..0206788 100644 --- a/Java/src/test/java/org/codecop/dependencies/e/CheckoutTest.java +++ b/Java/src/test/java/org/codecop/dependencies/e/CheckoutTest.java @@ -1,5 +1,7 @@ package org.codecop.dependencies.e; +import static org.mockito.Mockito.when; + import org.junit.Rule; import org.junit.Test; import org.mockito.Mock; @@ -14,14 +16,26 @@ public class CheckoutTest { @Mock private EmailService emailServiceMock; + @Mock + UserConfirmation newsLetter; + + @Mock + UserConfirmation terms; + @Test(expected = OrderCancelledException.class) - public void test5() { - System.out.println("note for tester:"); - System.out.println("* Accept Newsletter"); - System.out.println("* Do not Accept Terms"); + public void failIfUserDoesNotAcceptTerms() { + when(terms.isAccepted()).thenReturn(false); Product polkaDotSocks = new Product("Polka-dot Socks"); - Checkout checkout = new Checkout(polkaDotSocks, emailServiceMock); + Checkout checkout = new Checkout(polkaDotSocks, emailServiceMock) { + @Override + protected UserConfirmation createUserConfirmation(String message) { + if (message.startsWith("Subscribe")) { + return newsLetter; + } + return terms; + } + }; checkout.confirmOrder(); }