Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

A Tale About the Evil Partial Mock and Separation by Layers of Abstractions

4,897 views

Published on

We'll see that every partial mock is a wasted opportunity: instead of dropping a spy in there, think a bit about redesigning the system on finer-grained level of abstractions. Instead of mocking a method in your class, think of extracting it away in another class instead, and use a 'regular' mock.

These are the slides from my quickie session at Devoxx Poland 2017: https://youtu.be/pYG0jhCfT2A

Published in: Software
  • Be the first to comment

  • Be the first to like this

A Tale About the Evil Partial Mock and Separation by Layers of Abstractions

  1. 1. victor.rentea@gmail.com www.VictorRentea.ro @victorrentea#DevoxxPL Platinum Sponsor: A Tale About The Evil Partial Mock, And the Separation by Layers of Abstraction Victor Rentea !! HARD STUFF WARNING
  2. 2. VictorRentea.ro Spring, JavaEE, Hibernate Clean Code, Architectures Design Patterns TDD, Coach, Coding Dojos Java Performance, more... Victor Rentea Consultant, Technical Lead Lead Architect for major IBM client 2 Independent Trainer Clean Code Evangelist victor.rentea@gmail.com VictorRentea.ro@victorrentea 1000+ devs. 1500+ hrs. 5 yrs.
  3. 3. VictorRentea.ro Rules of Simple Design 3 - Kent Beck https://martinfowler.com/bliki/BeckDesignRules.html 2. Reveals Intention 3. No Duplication 4. Fewest Elements 1. Passes the Tests
  4. 4. VictorRentea.ro4 “Do TDD! 1. Passes the Tests
  5. 5. VictorRentea.ro Rules of Simple Design 5 https://martinfowler.com/bliki/BeckDesignRules.html 2. Reveals Intention 3. No Duplication 4. Fewest Elements 1. Passes the Tests
  6. 6. VictorRentea.ro6 “Write Expressive Code  Go to The Art of Clean Code Talk (today at 1630, Room 1) 2. Reveals Intention
  7. 7. VictorRentea.ro Rules of Simple Design 7 https://martinfowler.com/bliki/BeckDesignRules.html 2. Reveals Intention 3. No Duplication 4. Fewest Elements 1. Passes the Tests
  8. 8. VictorRentea.ro DIE 8 3. No Duplication
  9. 9. VictorRentea.ro DRY 9 Duplication Is Evil 3. No Duplication D I E
  10. 10. VictorRentea.ro SPOT 10 Don’t Repeat Yourself Duplication Is Evil 3. No Duplication D R Y
  11. 11. VictorRentea.ro11 Don’t Repeat Yourself Duplication Is Evil Single Point Of Truth 3. No Duplication D R Y S P O T
  12. 12. VictorRentea.ro Rules of Simple Design 12 https://martinfowler.com/bliki/BeckDesignRules.html 2. Reveals Intention 3. No Duplication 4. Fewest Elements 1. Passes the Tests
  13. 13. VictorRentea.ro13 Premature Overengineering 4. Fewest Elements Premature Optimization FEAR of new functionalities of not meeting the NFR Let’s write code, more code....
  14. 14. VictorRentea.ro Rules of Simple Design 14 https://martinfowler.com/bliki/BeckDesignRules.html 2. Reveals Intention 3. No Duplication 4. Fewest Elements 1. Passes the Tests
  15. 15. VictorRentea.ro Ordering the Rules of Simple Design 15 Reveals Intention No Duplication Fewest Elements Passes the Tests
  16. 16. VictorRentea.ro Ordering the Rules of Simple Design 16 Reveals Intention No Duplication Fewest Elements
  17. 17. VictorRentea.ro Ordering the Rules of Simple Design 17 Reveals Intention No Duplication Fewest Elements
  18. 18. VictorRentea.ro Ordering the Rules of Simple Design 18 Reveals Intention Fewest ElementsVS CustomerFacade saveCustomer() getCustomer() searchCustomer() saveCustomerPreferences() getCustomerPreferences() validateAddress() resetPassword() checkPassworStrength() CustomerPreferecesFacade saveCustomerPreferences() getCustomerPreferences() validateAddress() resetPassword() checkPassworStrength() CustomerFacade saveCustomer() getCustomer() searchCustomer() Same level of abstraction Huh?
  19. 19. VictorRentea.ro Ordering the Rules of Simple Design 19 Reveals Intention No Duplication Fewest Elements
  20. 20. VictorRentea.ro Ordering the Rules of Simple Design 20 No Duplication Fewest ElementsVS OrderService AlertService DeliveryService Different level of abstraction Extract for Reuse sendEmail() Wat!? Super-funny IT clip: https://www.destroyallsoftware.com/talks/wat
  21. 21. @ VictorRentea.ro In L/XL apps: hard to detect reuse opportunities  Pair Programming, design brainstorming 21 Extract for Reuse Increase the Bus Factor
  22. 22. @ VictorRentea.ro22 But what about The First Rule?
  23. 23. @ VictorRentea.ro23 One for All, All for One tests production
  24. 24. VictorRentea.ro24 OrderService EmailService PARTIAL MOCK (normal)MOCK private sendEmail() public sendEmail() Separation by Layers of Abstraction A Tale about Test Overlapping and the Evil Partial Mock... DrivenbyTests Refactor tests using mocks Which methods are more abstract ?Don’t do that !! Partial Mocks are Evil ! Oh!.. Why? Dunno.. Overlaping Tests !! public placeOrder() public shipOrder() a.k.a. “spy”
  25. 25. VictorRentea.ro25
  26. 26. VictorRentea.ro26 A Tale about Test Overlapping and the Evil Partial Mock... Morale Tempted to use a Partial Mock to reduce Test Overlapping ? Try to Separate By Layers of Abstraction Simple, better factored code/tests
  27. 27. VictorRentea.ro Rules of Simple Design 27 https://martinfowler.com/bliki/BeckDesignRules.html 2. Reveals Intention 3. No Duplication 4. Fewest Elements 1. Passes the Tests
  28. 28. victor.rentea@gmail.com www.VictorRentea.ro @victorrentea#DevoxxPL Platinum Sponsor: A Tale About The Evil Partial Mock, And the Separation by Layers of Abstraction Victor Rentea
  29. 29. victor.rentea@gmail.com www.VictorRentea.ro @victorrentea#DevoxxPL Platinum Sponsor: A Tale About The Evil Partial Mock, And the Separation by Layers of Abstraction Victor Rentea Expressive Code The Art of Clean Code Want more ? 16:30

×