Skip to content

Instantly share code, notes, and snippets.

@rchatley
Last active April 5, 2022 10:02
Show Gist options
  • Save rchatley/f5f30032e8ac7b2574d23cfc5c5b1d79 to your computer and use it in GitHub Desktop.
Save rchatley/f5f30032e8ac7b2574d23cfc5c5b1d79 to your computer and use it in GitHub Desktop.
HeadChef (mocks)
package restaurant;
public class HeadChef {
private final Chef pastryChef;
private final Waiter waiter;
public HeadChef(Chef pastryChef, Waiter waiter) {
this.pastryChef = pastryChef;
this.waiter = waiter;
}
public void order(Order main, Order dessert) {
pastryChef.order(dessert);
}
public void customerReadyFor(Order dish) {
if (pastryChef.isCooked(dish)) {
waiter.serve(dish);
}
}
}
package restaurant;
import org.jmock.Expectations;
import org.jmock.integration.junit4.JUnitRuleMockery;
import org.junit.Rule;
import org.junit.Test;
public class HeadChefTest {
static final Order APPLE_TART = new Order("apple tart");
static final Order ROAST_CHICKEN = new Order("chicken");
@Rule public JUnitRuleMockery context = new JUnitRuleMockery();
Chef pastryChef = context.mock(Chef.class);
Waiter waiter = context.mock(Waiter.class);
HeadChef chef = new HeadChef(pastryChef, waiter);
@Test
public void delegatesDessertsToPastryChef() {
context.checking(new Expectations() {{
exactly(1).of(pastryChef).order(APPLE_TART);
}});
chef.order(ROAST_CHICKEN, APPLE_TART);
}
@Test
public void asksWaiterToServeDessertIfReady() {
context.checking(new Expectations() {{
exactly(1).of(pastryChef).isCooked(APPLE_TART); will(returnValue(true));
exactly(1).of(waiter).serve(APPLE_TART);
}});
chef.customerReadyFor(APPLE_TART);
}
@Test
public void doesNotAskWaiterToServeDessertIfNotReady() {
context.checking(new Expectations() {{
exactly(1).of(pastryChef).isCooked(APPLE_TART); will(returnValue(false));
}});
chef.customerReadyFor(APPLE_TART);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment