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

Mockito 2022-01-25

sullis
January 25, 2022

Mockito 2022-01-25

Unit testing with Mockito
Portland Java User Group
January 25, 2022

#java
#mockito

sullis

January 25, 2022
Tweet

More Decks by sullis

Other Decks in Programming

Transcript

  1. Unit testing
    with Mockito
    Sean Sullivan
    Portland Java User Group
    January 25, 2022

    View full-size slide

  2. • Mockito library
    • Upgrading to Mockito 4
    • Extending Mockito
    • Anti-patterns

    View full-size slide

  3. Mockito is a framework
    for creating Mock objects

    View full-size slide

  4. a Mock object mimics the
    behavior of a real object

    View full-size slide

  5. Gilt.com
    checkout system

    View full-size slide

  6. We wrote unit tests to verify:
    • happy path
    • edge case scenarios
    • error handling logic

    View full-size slide

  7. • inventory reservation
    • payment system
    • shipping cost calculator
    • shipping restrictions
    • purchase limit service
    • tax calculator
    • discount calculator
    Gilt.com Checkout was dependent upon:

    View full-size slide

  8. The test suite for
    Gilt.com Checkout
    used Mockito

    View full-size slide

  9. We setup mock objects for:
    • Payment authorization
    • Inventory reservation
    • Discount redemption
    • Tax calculation
    • Shipping calculation

    View full-size slide

  10. Maven pom.xml

    org.mockito
    mockito-core
    4.3.1
    test

    View full-size slide

  11. import static org.mockito.Mockito.mock;
    import static org.mockito.Mockito.when;
    import static org.mockito.Mockito.verify;
    import static org.mockito.ArgumentMatcher.any;

    View full-size slide

  12. TaxEngine taxEngine = mock(TaxEngine.class);
    ShippingCostCalculator shippingCalc = mock(ShippingCostCalculator.class);
    when(taxEngine.calculate(any())).thenReturn(new Money(5, USD));
    when(shippingCalc.calculate()).thenReturn(shippingInfo);

    View full-size slide

  13. OrderManager unit test
    OrderManager manager = new OrderManager(
    taxEngine,
    shippingCalc);
    manager.sync(order);
    manager.submit(order);
    verify(taxEngine, once()).calculate(any());
    assertTrue(order.isSubmitted());

    View full-size slide

  14. Mockito.when

    View full-size slide

  15. How do I mimic a
    failure?

    View full-size slide

  16. use thenThrow

    View full-size slide

  17. TaxEngine taxEngine = mock(TaxEngine.class);
    when(taxEngine.calculate(any(), any()))
    .thenThrow(new TaxViolationException());

    View full-size slide

  18. Four major versions of Mockito
    • version 1.x
    • version 2.x
    • version 3.x
    • version 4.x

    View full-size slide

  19. Upgrading to
    Mockito 4.x

    View full-size slide

  20. async-http-client
    PR 1812

    View full-size slide

  21. async-http-client
    PR 1812

    View full-size slide

  22. async-http-client
    PR 1812

    View full-size slide

  23. async-http-client
    PR 1812

    View full-size slide

  24. async-http-client
    PR 1812

    View full-size slide

  25. What if
    my company has
    500 Git repos?

    View full-size slide

  26. Automated
    refactoring tool

    View full-size slide

  27. OpenRewrite
    https://moderne.io/

    View full-size slide

  28. Extending Mockito

    View full-size slide

  29. Answer interface
    public interface Answer {


    T answerInvocation(InvocationOnMock invocation);


    }


    View full-size slide

  30. MockitoPlus library

    View full-size slide

  31. MockitoPlus
    import static org.mockito.Mockito.mock;
    import static io.github.mockitoplus.MockitoPlus.when;

    View full-size slide

  32. MockitoPlus
    HelloWorld hello = mock(HelloWorld.class);
    when(hello.sayHello(any()))
    .thenReturn("bonjour")
    .failAlternatingInvocations();

    View full-size slide

  33. MockitoPlus
    HelloWorld hello = mock(HelloWorld.class);
    when(hello.sayHello(any()))
    .thenReturn("bonjour")
    .
    fi
    rstInvocationFails();

    View full-size slide

  34. MockitoPlus
    HelloWorld hello = mock(HelloWorld.class);
    when(hello.sayHello(any()))
    .thenReturn("bonjour")
    .intermittentFailures()
    .randomDelay(Duration.of(500, MILLIS));
    Intermittent failures
    with random delays

    View full-size slide

  35. Mockito anti-patterns

    View full-size slide

  36. // anti-pattern
    List names = mock(List.class);
    // better
    List names = new ArrayList();
    anti-pattern

    View full-size slide

  37. // anti-pattern
    Optional name = mock(Optional.class);
    // better
    Optional name = Optional.of(“Obama”);
    anti-pattern

    View full-size slide

  38. // anti-pattern
    CompletableFuture f = mock(CompletableFuture.class);
    // better
    CompletableFuture f = CompletableFuture.completedFuture(“bonjour”);
    anti-pattern

    View full-size slide

  39. what about Scala?

    View full-size slide

  40. “org.mockito” %% "mockito-scala" % “1.16.34” % Test
    build.sbt

    View full-size slide

  41. import org.mockito.scalatest.MockitoSugar
    Scala trait

    View full-size slide

  42. The End
    @tinyrobots

    View full-size slide