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
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. 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
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. @
VictorRentea.ro
In L/XL apps: hard to detect reuse opportunities
Pair Programming, design brainstorming
21
Extract for Reuse
Increase the Bus Factor
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”
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. 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