Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test doubles (Java/Mockito)

Test doubles (Java/Mockito)

Slides from coding dojo talk at work

Kerry Buckley

February 04, 2014
Tweet

More Decks by Kerry Buckley

Other Decks in Programming

Transcript

  1. TDD

  2. TDD

  3. TDD

  4. A stub simply returns canned values ! (which may be

    real value objects or other test doubles)
  5. Identify responsibilities Does the class have a single responsibility? Yes

    test drive behaviour as normal No identify collaborating roles (interfaces) specify interactions using test doubles
  6. Implement collaborators For each role identified at the layer above…

    Identify responsibilities Does the class have a single responsibility? Yes test drive behaviour as normal No identify collaborating roles (interfaces) specify interactions using test doubles
  7. Rules* A class can arrange collaborators or do work Stub

    queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour)
  8. Rules* A class can arrange collaborators or do work Stub

    queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives
  9. Rules* A class can arrange collaborators or do work Stub

    queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives Don’t stub or mock the object under test
  10. Rules* A class can arrange collaborators or do work Stub

    queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives Don’t stub or mock the object under test
  11. Rules* A class can arrange collaborators or do work Stub

    queries; mock commands Doubles can return value objects or other doubles (but not objects with behaviour) Don’t stub or mock value objects or primitives Don’t stub or mock the object under test *more what you’d call ‘guidelines’ than actual rules
  12. Stubbing import static org.mockito.Mockito.*; Widget widget = mock(Widget.class); when(widget.foo(0)).
 thenReturn("something");

    when(widget.foo(anyInt())).
 thenReturn("something else"); when(widget.bar(any())).
 thenThrow(new RuntimeException());
  13. Mocking import static org.mockito.Mockito.*; Widget widget = mock(Widget.class); // do

    some stuff verify(widget).foo(1); verify(widget).bar();
  14. Mocking import static org.mockito.Mockito.*; Widget widget = mock(Widget.class); // do

    some stuff verify(widget).foo(1); verify(widget).bar(); verify(widget).baz(42, isA(Quuz.class));
  15. Exercise Use doubles to test-drive a Transaction class with a

    transfer method that takes an amount and two Account objects (source and destination). When transfer is called, the source account should receive debit(amount) and the destination account credit(amount).