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

Mockito 2022-01-25

99d4c0ed3c8027ea1c37b53d6441d042?s=47 sullis
January 25, 2022

Mockito 2022-01-25

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

#java
#mockito

99d4c0ed3c8027ea1c37b53d6441d042?s=128

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
  2. Unit Tests

  3. • Mockito library • Upgrading to Mockito 4 • Extending

    Mockito • Anti-patterns
  4. Mockito is a framework for creating Mock objects

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

  6. Gilt.com checkout system

  7. We wrote unit tests to verify: • happy path •

    edge case scenarios • error handling logic
  8. • inventory reservation • payment system • shipping cost calculator

    • shipping restrictions • purchase limit service • tax calculator • discount calculator Gilt.com Checkout was dependent upon:
  9. The test suite for Gilt.com Checkout used Mockito

  10. We setup mock objects for: • Payment authorization • Inventory

    reservation • Discount redemption • Tax calculation • Shipping calculation
  11. Maven pom.xml <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> <version>4.3.1</version> <scope>test</scope> </dependency>

  12. import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.verify; import

    static org.mockito.ArgumentMatcher.any;
  13. TaxEngine taxEngine = mock(TaxEngine.class); ShippingCostCalculator shippingCalc = mock(ShippingCostCalculator.class); when(taxEngine.calculate(any())).thenReturn(new Money(5,

    USD)); when(shippingCalc.calculate()).thenReturn(shippingInfo);
  14. OrderManager unit test OrderManager manager = new OrderManager( taxEngine, shippingCalc);

    manager.sync(order); manager.submit(order); verify(taxEngine, once()).calculate(any()); assertTrue(order.isSubmitted());
  15. Mockito.when

  16. thenReturn

  17. How do I mimic a failure?

  18. use thenThrow

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

  20. Four major versions of Mockito • version 1.x • version

    2.x • version 3.x • version 4.x
  21. Upgrading to Mockito 4.x

  22. None
  23. None
  24. None
  25. None
  26. async-http-client PR 1812

  27. async-http-client PR 1812

  28. async-http-client PR 1812

  29. async-http-client PR 1812

  30. async-http-client PR 1812

  31. What if my company has 500 Git repos?

  32. Automated refactoring tool

  33. OpenRewrite https://moderne.io/

  34. OpenRewrite

  35. OpenRewrite

  36. mockito.yml

  37. Extending Mockito

  38. Answer interface public interface Answer<T> { T answerInvocation(InvocationOnMock invocation); }

  39. MockitoPlus library

  40. MockitoPlus

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

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

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

  44. MockitoPlus HelloWorld hello = mock(HelloWorld.class); when(hello.sayHello(any())) .thenReturn("bonjour") .intermittentFailures() .randomDelay(Duration.of(500, MILLIS));

    Intermittent failures with random delays
  45. Mockito anti-patterns

  46. // anti-pattern List<String> names = mock(List.class); // better List<String> names

    = new ArrayList<String>(); anti-pattern
  47. // anti-pattern Optional<String> name = mock(Optional.class); // better Optional<String> name

    = Optional.of(“Obama”); anti-pattern
  48. // anti-pattern CompletableFuture<String> f = mock(CompletableFuture.class); // better CompletableFuture<String> f

    = CompletableFuture.completedFuture(“bonjour”); anti-pattern
  49. what about Scala?

  50. None
  51. “org.mockito” %% "mockito-scala" % “1.16.34” % Test build.sbt

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

  53. Questions?

  54. The End @tinyrobots