Skip to content

Commit

Permalink
Extract and override factory function to control UI.
Browse files Browse the repository at this point in the history
  • Loading branch information
codecop committed Feb 13, 2024
1 parent 55372d6 commit 41179dd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 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
22 changes: 18 additions & 4 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
private UserConfirmation newsletter;

@Mock
private 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");
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("Accept")) {
return terms;
}
return newsletter;
}
};

checkout.confirmOrder();
}
Expand Down

0 comments on commit 41179dd

Please sign in to comment.