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

Too many mocks killed the test - Sw Crafters Lyon 2021

Too many mocks killed the test - Sw Crafters Lyon 2021

64822af642dacc617380f009e35a78f7?s=128

Jean-Marie Lamodière

May 26, 2021
Tweet

More Decks by Jean-Marie Lamodière

Other Decks in Programming

Transcript

  1. Too many mocks killed the test What Hexagonal Architecture has

    changed Icons made by Icongeek26, Freepik from www.flaticon.com Jean-Marie Lamodière - Backend tech lead @jmlamodiere @swcraftlyon Slow Fast, Focus
  2. WHY ? Locking Tests vs. TDD Development Locking Tests Development

    TDD +50% Icons made by Roundicons from www.flaticon.com “I mock almost nothing” - Kent Beck https://youtu.be/z9quxZsLcfo?t=1270
  3. WHY ? Kurt Lewin Change Theory https://sanzubusinesstraining.com/kurt-lewin-change-model/ “...but everyone test

    like that” - Your colleagues
  4. WHY ? Icons made by Freepik from www.flaticon.com Refactor locking

    tests https://github.com/JMLamodiere/tdd-demo-forumphp2020 POO, sync 2 demo projects Tests first https://github.com/JMLamodiere/tdd-demo-swcraftlyon-scala POO, async
  5. WHY ? Icons made by Freepik from www.flaticon.com https://github.com/JMLamodiere/tdd-demo-forumphp2020 /Application

    /Command RegisterRunningSession RegisterRunningSessionHandler /Domain WeatherProvider(Interface) RunningSession RunningSessionRepo(Interface) /Infrastructure /Http HttpAccuWeatherProvider CurrentConditionDeserializer /Database PostgresRunningSessionRepo /Infrastructure /Symfony RunningSessionController RegisterRunningSessionDeserializer RunningSessionSerializer Technical Business Technical
  6. WHY ? Icons made by Freepik from www.flaticon.com https://github.com/JMLamodiere/tdd-demo-swcraftlyon-scala /Application

    /Command RegisterRunningSession RegisterRunningSessionHandler /Domain WeatherProvider(Interface) RunningSession RunningSessionRepo(Interface) /Infrastructure /Http HttpAccuWeatherProvider /Database PostgresRunningSessionRepo /Infrastructure /AkkaHttp RunningSessionPostController JsonSupport Technical Business Technical
  7. WHY ? ...But that’s DDD? No it’s hexagonal! https://afup.org/talks/3037-de-crud-a-ddd-comment-meetic-a-sauve-son-legacy 4k

    vues 😱
  8. • A Functional Test describes a behaviour • A Unit

    Test locks an implementation Prejudice #1 Icons made by Freepik from www.flaticon.com Could you review my PR before I write my Unit Tests ?
  9. • A Functional Test describes a behaviour • A Unit

    Test locks an implementation Prejudice #1 Icons made by Roundicons from www.flaticon.com All tests describe an expected public behaviour Only the audience and the size of the covered area vary Test Desiderata - Kent Beck : https://kentbeck.github.io/TestDesiderata/
  10. “Unit Test” = 1 method of 1 class Prejudice #2

    Icons made by Freepik from www.flaticon.com
  11. “Unit Test” = 1 method of 1 class Prejudice #2

    Icons made by Roundicons, Smashicons from www.flaticon.com Polysemy! Wrong debate... “A DeveloperTest is the correct name for what the industry generally calls a UnitTest” - Ward Cunningham The Unit Test trap : https://drpicox.medium.com/the-unit-test-trap-4a83e4012b17
  12. • Test Pyramid = big Unit Test coverage • Unit

    Test = mock other classes Prejudice #3 Icons made by Freepik from www.flaticon.com
  13. • Test Pyramid = big Unit Test coverage • Unit

    Test = mock other classes Prejudice #3 Icons made by Roundicons, Smashicons from www.flaticon.com Favor fast and focused tests. Ex: The Practical Test Pyramid : https://martinfowler.com/articles/practical-test-pyramid.html Slow Fast, Focus Sociable Unit Test UI component test Unit Tests on Adapters
  14. Only functional tests have 3 steps: • Given • When

    • Then Prejudice #4 Icons made by Freepik from www.flaticon.com
  15. Only functional tests have 3 steps: • Given • When

    • Then Prejudice #4 Icons made by Roundicons from www.flaticon.com All tests follow the narrative structure: • Given = Arrange • When = Act • Then = Assert Setup Catalyst Resolution
  16. Prejudice #5 Icons made by Freepik from www.flaticon.com Checking the

    number of calls to all mocked methods increases my confidence. ->expects($this->exactly(1))->method('xxx') ->shouldBeCalledTimes(1)
  17. Prejudice #5 Checking the number of calls to all mocked

    methods increases my confidence ->expects($this->exactly(1))->method('xxx') ->shouldBeCalledTimes(1) Icons made by Roundicons, Smashicons from www.flaticon.com • Blurs the intent of the test • Locks implementation details • Impossible to write first
  18. WHY ? What to test ? Subject Under Test (S.U.T.)

    Arguments (IN) Return value (OUT) Test Calls Collaborator (IN) Collaborator (OUT) Assert Stub : willReturn() Spy : shouldHaveBeenCalled() Given When Then Then
  19. I must know the splitting between my classes before I

    write my tests Prejudice #6 Icons made by Freepik from www.flaticon.com
  20. I must know the splitting between my classes before I

    write my tests Prejudice #6 Icons made by Roundicons from www.flaticon.com • Prod classes splitting != test classes splitting • Start with an area with a known boundary
  21. WHY ? Icons made by Freepik from www.flaticon.com /Application /Command

    RegisterRunningSession RegisterRunningSessionHandler /Domain WeatherProvider(Interface) RunningSession RunningSessionRepo(Interface) /Infrastructure /Http HttpAccuWeatherProvider CurrentConditionDeserializer /Database PostgresRunningSessionRepo /Infrastructure /Symfony RunningSessionController RegisterRunningSessionDeserializer RunningSessionSerializer Technical Business Technical 1st test: area with a known boundary
  22. Prejudice #7 Icons made by Freepik from www.flaticon.com If I

    don't mock my Entities / DTOs, too many tests will break if I add a field
  23. Prejudice #7 Icons made by Roundicons from www.flaticon.com If I

    don't mock my Entities / DTOs, too many tests will break if I add a field • Use test Factori(es) with optional fields • Getter not covered ? I remove it
  24. Prejudice #8 Icons made by Freepik from www.flaticon.com I can

    mock the libraries used on the Infrastructure side : • Http : Guzzle… • Database : Doctrine, Eloquent…
  25. Prejudice #8 Icons made by Roundicons, Smashicons from www.flaticon.com I

    can mock the libraries used on the Infrastructure side : • Http : Guzzle… • Database : Doctrine, Eloquent… • Coupled with the usage details of the lib • Assumptions about the way it behaves • Impossible to write first • No confidence when upgrading the lib
  26. WHY ? Infrastructure : Integration Tests Icons made by Freepik,

    Smashicons, Roundicons from www.flaticon.com QueryBuilder -> pure SQL MysqlRepository Docker mysql HttpRepository Wiremock Guzzle -> Symfony HttpClient Refactoring eg.
  27. WHY ? “Don’t mock what you don’t own” Icons made

    by Freepik, Smashicons, Roundicons from www.flaticon.com /Application /Command RegisterRunningSession RegisterRunningSessionHandler /Domain WeatherProvider(Interface) RunningSession RunningSessionRepo(Interface) /Infrastructure /Http HttpAccuWeatherProvider CurrentConditionDeserializer /Database PostgresRunningSessionRepo Unit/Fast/Small tests Integration Mock No Mock!
  28. Prejudice #9 Icons made by Freepik from www.flaticon.com Functional Test

    = necessarily end to end
  29. Prejudice #9 Icons made by Roundicons, smashicons from www.flaticon.com Functional

    Test = necessarily end to end • Slow • Brittle • Difficult to maintain • Harder to write first • Locks Infrastructure choices too soon Suggestion : mock Secondary Ports
  30. WHY ? Icons made by Freepik from www.flaticon.com /Application /Command

    RegisterRunningSession RegisterRunningSessionHandler /Domain WeatherProvider(Interface) RunningSession RunningSessionRepo(Interface) /Infrastructure /Http HttpAccuWeatherProvider CurrentConditionDeserializer /Database PostgresRunningSessionRepo /Infrastructure /Symfony RunningSessionController RegisterRunningSessionDeserializer RunningSessionSerializer Suggestion Functional Integration
  31. WHY ? Icons made by Freepik from www.flaticon.com Suggestion Functional

    Integration Outside-in Diamond 🔷 TDD - Thomas Pierrain http://tpierrain.blogspot.com/2021/03/outside-in-diamond-tdd-1-style-made.html
  32. @jmlamodiere Thanks! Development Locking tests Development TDD +50% Icons made

    by Roundicons, Freepik from www.flaticon.com It’s your turn ! • //Given //When //Then • Even if I write a test afterwards, I only write what I would have been sure of before https://github.com/JMLamodiere/tdd-demo-forumphp2020 https://github.com/JMLamodiere/tdd-demo-swcraftlyon-scala @swcraftlyon