Skip to content

Instantly share code, notes, and snippets.

@mgenov
Created January 20, 2012 14:50
Show Gist options
  • Save mgenov/1647672 to your computer and use it in GitHub Desktop.
Save mgenov/1647672 to your computer and use it in GitHub Desktop.
MockitoBehaviorTest.java
public class MockitoBehaviorTest {
interface Registry {
void register(Double amount);
}
class PaymentProcessingListener {
private final Registry registry;
PaymentProcessingListener(Registry registry) {
this.registry = registry;
}
public void onPaymentProcessed(Double amount) {
if (amount == 0d) {
return;
}
registry.register(amount);
}
}
@Test
public void processRegularPayment() {
final Registry registry = mock(Registry.class);
PaymentProcessingListener listener = new PaymentProcessingListener(registry);
listener.onPaymentProcessed(20d);
verify(registry).register(20d);
}
@Test
public void zeroPaymentsAreNotRegistered() {
Registry registry = mock(Registry.class);
PaymentProcessingListener listener = new PaymentProcessingListener(registry);
listener.onPaymentProcessed(0d);
verifyZeroInteractions(registry);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment