Skip to content

Commit

Permalink
Extract and Override Factory Method to control new UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecop committed Feb 13, 2024
1 parent e4337ef commit 5e49dfa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
8 changes: 6 additions & 2 deletions Java/src/main/java/org/codecop/dependencies/e/Checkout.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 19 additions & 5 deletions Java/src/test/java/org/codecop/dependencies/e/CheckoutTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
Expand Down

0 comments on commit 5e49dfa

Please sign in to comment.