SlideShare une entreprise Scribd logo
1  sur  51
Your tests are trying
to tell you something ...
10 design hints you were missing
Read the article:
https://victorrentea.ro/blog/design-insights-from-unit-testing/
About the speaker:
https://victorrentea.ro
victorrentea.ro/training-offer
Hi, I'm Victor Rentea 🇷🇴
Java Champion, 17 years of code, code, code, code, code....
Consultant & Trainer: 5000 developers of 100+ companies in EU:
❤️ Clean Code, Architecture, Unit Testing
🛠 Spring Framework, Hibernate/JPA, Reactive/WebFlux
⚡️ Java Performance, Secure Coding 🔐
Conference Speaker – find many recoded talks online
Founder of Bucharest Software Crafters Community: 5000+ members
🔥 Free monthly Zoom webinars, 1-2 hours after work. Join at victorrentea.ro/community
Past events on my channel: youtube.com/vrentea
Father of 👧👦, woke up at night by a 😺: VictorRentea.ro
3 VictorRentea.ro
From the Agile Ideology ...
Emergent Design
while we keep shipping shit working software fast,
the design of the system will naturally evolve by itself
(as opposed to large up-front design that can cause overengineering)
4 VictorRentea.ro
Writing Tests give you hints
on when to improve the design
Emergent Design
that never emerged
😩
We need triggers!
When should I refine the design?
5 VictorRentea.ro
Kent Beck
Creator of Extreme Programming (XP)
a very technical form of Agile
Inventor of TDD
Author of JUnit
Father of Unit Testing
6 VictorRentea.ro
1. Passes all Tests 💪
2. Expresses Intent = SRP, Domain Names
2. No Duplication = DRY🌵
3. Keep it Simple = KISS💋
Rules of Simple Design
by Kent Beck
https://martinfowler.com/bliki/BeckDesignRules.html
 design feedback 💎
7 VictorRentea.ro
Testing in many Successful Monoliths :
Ice-Cream Cone Testing Anti-Pattern
30 manual testers
🧔🏼♂️🧔🏼♂️🧔🏼♂️🧔🏼♂️ 🧔🏼♂️🧔🏼♂️🧔🏼
20% coverage (GUI robots, Selenium,..)
absent (old frameworks)
scarce, 10% coverage
8 VictorRentea.ro
https://martinfowler.com/articles/practical-test-pyramid.html
E2E
More design feedback
Testing Pyramid
Test a thin slice of behavior
MOCKS
Test a group of modules
Test everything as whole
⭐️ Deep Edge Case
⭐️ Critical Business Flow (eg. checkout)
More busine$$ safety
overlapping is
normal to happen
9 VictorRentea.ro
MOCKS
💖 or 🤬
10 VictorRentea.ro
Why we 💖 Mocks
Isolated Tests
from external systems 🤔
Fast 🐇
no framework, DB, external APIs
Simpler
when testing high complexity 😵💫 
Alternatives:
in-mem DB
Testcontainers 🐳
WireMock
Cheating*
* James Coplien in https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf
11 VictorRentea.ro
Logic
under test
Cyclomatic Complexity
= number of independent execution paths through code ≈ max no. tests
Test
Test
Test
Test
Test
Test
Test
CC=5 CC=6
f(a) g(b)
calls
f() calling g() together have a CC =
Many
To cover all branches
Heavy
setup and input data
Tests for a+b become...
3
0
12 VictorRentea.ro
MOCK
13 VictorRentea.ro
Why we 🤔 Mocks
Uncaught Bugs 😱
despite 1000s of green✅ tests: lock, tap, doors, umbrella 🧔
Fragile Tests 💔
that break when refactoring internals
Unreadable Tests 😵💫
A test using 5+ mocks 
14 VictorRentea.ro
15 VictorRentea.ro
The test has 20 lines full of mocks
😵💫
😡
BURN THE TEST!
has a bad cost/benefit ratio
HONEYCOMB TESTING
Outside-in from integration- to unit- test
WHAT AM I TESTING HERE ?
syndrome
Tested prod code has 4 lines
😩
f(..) {
a = api.fetchB(repoA.find(..).getBId());
d = service.createD(a,b,repoC.find(..));
repo3.save(d);
mq.send(d.id);
}
REDESIGN PRODUCTION
Collapse Middle-Man / useless layer
16 VictorRentea.ro
https://martinfowler.com/articles/practical-test-pyramid.html
End-to-end
The Testing Pyramid
17 VictorRentea.ro
The Bad Monolith ("Big ball of mud") has
- Highly coupled code
- Huge complexity behind a few entry points  the need for lots of Unit Tests 
Microservices / Modulith
- Huge complexity in a single microservice = bad practice  Break µservice/module
- Many APIs, hiding a decent amount of complexity (🙏)
- Easier to test more at the API level  Honeycomb Testing Strategy
Testing Microservices is different
18 VictorRentea.ro
Integration
test the entire microservice in isolation
Integrated
may fail because of another system
Implementation Detail
complex parts of the code
Start up all microservices in the ecosystem.
Expensive, flaky, but business-critical e2e tests (eg checkout)
Responsibility of a central QA team(?)
Test end-to-end as many flows in your system.
Starts up the entire system (@SpringBootTest)
 Keep tests isolated without mocks
Separate tests for the parts of the code naturally isolated
with high internal complexity.
Mocks are allowed
Still: test roles, not methods/classes
Write social unit tests
Honeycomb Testing Strategy
(suitable for microservices)
Testcontainers 🐳
WireMock
DB ES Kafka ...
API
many tests on
one entrypoint
decouple and test alone
https://engineering.atspotify.com/2018/01/testing-of-microservices/
19 VictorRentea.ro
⚠️
Test manageable complexity without mocks
A B
Instead, test "components" (groups of objects)
#0
Internal refactoring won't break tests
20 VictorRentea.ro
You get most design feedback
from tests in 'Implementation Detail'
Implementation
Detail
21 VictorRentea.ro
Unit Testing puts more design pressure on
methods and data structures
#1
22 VictorRentea.ro
var bigObj = new BigObj();
bigObj.setA(a);
bugObj.setB(b);
prod.method(bigObj);
Tests must create bigObj just to pass two inputs🧔
method(bigObj)
MUTABLE
DATA in 2023?
using only 2 of the 15 fields in bigObj
method(a, b)
Precise Signatures prod.method(a, b);
Also, simpler tests:
Pass only necessary data to functions ✅
when(bigObj.getPart1())
.thenReturn(p1);
⛔️ Don't Mock Getters ⛔️
Mock behavior, not data
prod.method(new AB(a, b));
method(ab)
Parameter Object
Testing highly complex logic:
⛔️ Don't have Mocks return Mocks⛔️
23 VictorRentea.ro
🏰
Constrained Objects
= data structures that guard their internal consistency by throwing exceptions,
(eg required fields, string size / regex, or more complex domain rules)
 Mutable (eg Domain Entities, Aggregates)
via constructor and setters/mutator methods
 Immutable❤️ (Value Objects)
via constructor
24 VictorRentea.ro
A group of classes has a
clear role and manageable complexity (A)
An Object is Constrained ✅ but Large (B)
Object Mother Pattern*
eg. TestData.john(): Customer
coupling
Break Domain Entities
in separate Bounded Contexts
packages > modulith > microservices
 Test the entire group with a social unit-test✅:
requires larger setup and larger input
* https://martinfowler.com/bliki/ObjectMother.html (2006)
Same test data factory used in different verticals
customer | order
↓
Break Object Mother per vertical
CustomerTestData | OrderTestData
Creating valid test data gets cumbersome
CREEPY
A shared class creating valid test objects
26 VictorRentea.ro
Your complex logic directly uses
APIs or heavy libraries: Unit-testing your logic
requires understanding the semantics of:
The External API: to populate/assert DTOs
The Library: to mock it
... api.call(apiDetails);
... dto.getStrangeField()
... Lib.use(mysteriousParam,...)
Unit Tests speak your Domain Model
Unit Tests are first-class citizens of your project
#respect them
Agnostic Domain
Isolate complex logic from the outside world
... clientAdapter.call(domainStuff)
... domainObject.getMyField()
... libAdapter.use(😊)
application / infra
Value Object
Entity
id
Domain
Service
Domain
Service
agnostic
domain
My DTOs
External
API
External
DTOs
Clien
t
External
Systems
Façade
Controller Repo
IAdapter
Adapter
⛔️
⛔️
Deep complexity is kept inside
for easier testing
Simplified Onion Architecture
Interf
Ugly
Invasive
Library
28 VictorRentea.ro
#1 Unit Testing loves
precise signatures
smaller data structures
agnostic domain
29 VictorRentea.ro
#2 Unit Testing puts design pressure on
Highly Complex Logic
🤔
30 VictorRentea.ro
class Big {
f() { //complex
g();
}
g() { //complex
}
}
Inside the same class,
a complex function f()
calls a complex g()
g() is complex => unit-tested separately
When testing f(), can I avoid executing g()?
That is: can I mock a local method call?
class HighLevel {
LowLevel low;
f() {//complex
low.g();
}
} class LowLevel {
g() {/*complex*/}
}
↓
Partial Mock (@Spy)
Hard to maintain tests:
Which method is real, which is mocked?🧔
Split by Layers of Abstraction
(high-level policy vs low-level details)
Tolerable testing Legacy Code
If splitting the class doesn't feel right,
test f+g together with bigger tests
31 VictorRentea.ro
class HighLevel {
LowLevel low;
f() {//complex
low.g();
}
}
Split by Layers of Abstraction
= vertical split of a class
class LowLevel {
g() {/*complex*/}
}
32 VictorRentea.ro
class Wide {
A a;
B b; //+more dependencies
complexA() {..a.fa()..}
complexB() {..b.fb()..}
}
@ExtendWith(MockitoExtension)
class WideTest {
@Mock A a;
@Mock B b;
@InjectMocks Wide wide;
// 5 tests for complexA()
// 4 tests for complexB()
}
↓
Separate test classes: ComplexATest, ..B..
(keep before useful for all tests 👌)
Complex methods in the same class
use different sets of dependencies:
class ComplexA {
A a; // +more
complexA() {
..a.fa()..
}
}
class ComplexB {
B b; // +more
complexB() {
..b.fb()..
}
}
@BeforeEach
void fixture() {
when(a.fa()).then...
when(b.fb()).then...
}
Split Unrelated Complexity not used when
testing complex1()
what part of the before
is used by my failed test?
** Mockito (since v2.0) throws UnnecessaryStubbingException if a when..then is not used by a @Test, when using MockitoExtension
** This is
BIG
🧔
FIXTURE CREEP
test setup
DRY Principle
33 VictorRentea.ro
Split Unrelated Complexity
= horizontal split a class
class ComplexA {
A a; // +more
complexA() {
..a.fa()..
}
}
class ComplexB {
B b; // +more
complexB() {
..b.fb()..
}
}
34 VictorRentea.ro
vertically ↕️
Tests give us hints on how to break complexity
horizontally ↔️
When should we follow those hints?
35 VictorRentea.ro
Mock Roles, not Objects
http://jmock.org/oopsla2004.pdf
The dawn of Mocks (2004):
36 VictorRentea.ro
The dawn of Mocks (2004):
37 VictorRentea.ro
BAD HABIT
Mock Roles, not Objects
http://jmock.org/oopsla2004.pdf
You implement a new feature
> ((click in UI/postman)) > It works > Yee-Haa!
OMG, I forgot about unit-tests 😱
...then you write unit tests
by mocking all dependencies
of the prod class you wrote
Several years later, you complain that
your tests are fragile and impede refactoring
Contract-Driven Design
Before mocking a dependency,
clarify its responsibility
=
Changing an API you mocked is painful
😭
38 VictorRentea.ro
"Unit Testing means mocking all dependencies of a class"
- common belief
"It's perfectly fine for unit tests to talk to databases and filesystems!"- Ian Cooper
Unit Testing
= ?
39 VictorRentea.ro
timeframe for developing your feature
When do you start writing tests?
✅ understand the problem => early questions to biz
✅ early design feedback 💎 💎 💎
✅ real test coverage => courage to refactor
40 VictorRentea.ro
#2 Unit Testing help us break complexity:
Horizontally
Vertically
By Roles
41 VictorRentea.ro
#3 Unit Testing promotes
Functional Programming
pure functions & immutable objects
42 VictorRentea.ro
1) Has no Side-Effects
(doesn't change anything)
INSERT, POST, send message, field changes, files
2) Same Inputs => Same Output
(no external source of data)
GET, SELECT, current time, random, …
Pure Function
aka "Referential Transparency"
43 VictorRentea.ro
No Network or files
No Changes to Data
No time/random
Pure Functions
use immutable objects❤️
they are super fast
(simplified definition)
44 VictorRentea.ro
a = repo1.findById(..)
b = repo2.findById(..)
c = api.call(..)
🤯complexity🤯
repo3.save(d);
mq.send(d.id);
Very complex logic
using many dependencies
(eg: computePrice, applyDiscounts)
Many tests
using heavy mocking
when(..).thenReturn(a);
when(..).thenReturn(b);
when(..).thenReturn(c);
prod.complexAndCoupled();
verify(..).save(captor);
verify(..).send(...);
x15=😖
Easier to test w/ less mocks ✅
d = prod.pure(a,b,c);
assertThat(d)...
Reduce Coupling of Complex Logic
D pure(a,b,c) {
🤯complexity🤯
return d;
}
Easier to understand ✅
45 © VictorRentea.ro
a training by
Complexity
Functional Core
Imperative Shell / Functional Core Segregation
State Mutation
DB
Imperative Shell
API call
Files
Complex Logic
46 © VictorRentea.ro
a training by
Functional
Core
Imperative Shell
Imperative Shell / Functional Core Segregation
47 VictorRentea.ro
method(Mutable order, d) {
ds.applyDiscounts(order, d);
var price = cs.computePrice(order);
return price;
}
... but you use mutable objects
Swapping two lines can still cause bugs
that is: 4000 ✅ tests, ❌ bugs in production
You have 4.000 unit tests,
100% test coverage 😲
👏
↓
Paranoid Testing
(verifying method call order)
Immutable Objects
method(Immutable order, d) {
var discountedOrder = ds.applyDiscounts(order, d);
var price = cs.computePrice(discountedOrder);
return price;
}
TEMPORAL
COUPLING
Swapping two lines
❌ does not compile
48 VictorRentea.ro
1. Collapse Middle-Man vs "What am I testing here?" Syndrome
2. Honeycomb Testing Strategy vs Fragile microscopic Unit Tests
3. Precise Signatures
4. Tailored Data Structures vs Creepy Object Mother
5. Keep complexity inside Agnostic Domain not on APIs or Extensive
Libraries
6. Separate Complexity by Layers of Abstraction ↕️ vs @Spy
7. Separate Unrelated Complexity ↔️ vs Fixture Creep (bloated setup)
8. Refine Roles (Mock Roles, not Objects) vs blindly @Mock all dependencies
9. More Complexity => Less Dependencies vs Mock-full tests
10.Promote Immutable Objects vs Temporal Coupling
Design Hints from Tests
49 VictorRentea.ro
Testable Design
is Good Design
Writing fine-grained unit early
increases friction with bad design
51 VictorRentea.ro
Unit Testing Reading Guide
1] Classic TDD⭐️⭐️⭐️ (mock-less) https://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530
Mock Roles, not Objects ⭐️⭐️⭐️: http://jmock.org/oopsla2004.pdf
"Is TDD Dead?" https://martinfowler.com/articles/is-tdd-dead/
Why Most Unit Testing is Waste (James Coplien): https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf
vs Integrated Tests are a Scam(J Brains): https://blog.thecodewhisperer.com/permalink/integrated-tests-are-a-scam
2] London TDD⭐️⭐️⭐️ (mockist) https://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627
3] Patterns⭐️ https://www.amazon.com/Art-Unit-Testing-examples/dp/1617290890
4] https://www.amazon.com/xUnit-Test-Patterns-Refactoring-Code/dp/0131495054/
5] (skip through) https://www.amazon.com/Unit-Testing-Principles-Practices-Patterns
52 VictorRentea.ro
Write more automated tests,
earlier, to have time to listen to
what tests are trying to tell you
Join my community to keep in touch,
FOR free monthly, ONLINE debates :
victorrentea.ro

Contenu connexe

Tendances

Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the DomainVictor Rentea
 
The Proxy Fairy, and The Magic of Spring Framework
The Proxy Fairy, and The Magic of Spring FrameworkThe Proxy Fairy, and The Magic of Spring Framework
The Proxy Fairy, and The Magic of Spring FrameworkVictor Rentea
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean codeVictor Rentea
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing ArchitecturesVictor Rentea
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionsaber tabatabaee
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APIMario Fusco
 
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideEvolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideVictor Rentea
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Victor Rentea
 
Functional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupFunctional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupVictor Rentea
 
Clean Code - The Next Chapter
Clean Code - The Next ChapterClean Code - The Next Chapter
Clean Code - The Next ChapterVictor Rentea
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programmingRodolfo Finochietti
 
Vert.x for Microservices Architecture
Vert.x for Microservices ArchitectureVert.x for Microservices Architecture
Vert.x for Microservices ArchitectureIdan Fridman
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented ProgrammingScott Wlaschin
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean ArchitectureMattia Battiston
 

Tendances (20)

Clean architecture - Protecting the Domain
Clean architecture - Protecting the DomainClean architecture - Protecting the Domain
Clean architecture - Protecting the Domain
 
The Proxy Fairy, and The Magic of Spring Framework
The Proxy Fairy, and The Magic of Spring FrameworkThe Proxy Fairy, and The Magic of Spring Framework
The Proxy Fairy, and The Magic of Spring Framework
 
The Art of Clean code
The Art of Clean codeThe Art of Clean code
The Art of Clean code
 
Vertical Slicing Architectures
Vertical Slicing ArchitecturesVertical Slicing Architectures
Vertical Slicing Architectures
 
clean code book summary - uncle bob - English version
clean code book summary - uncle bob - English versionclean code book summary - uncle bob - English version
clean code book summary - uncle bob - English version
 
Let's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java APILet's make a contract: the art of designing a Java API
Let's make a contract: the art of designing a Java API
 
Clean code
Clean codeClean code
Clean code
 
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideEvolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
 
Clean coding-practices
Clean coding-practicesClean coding-practices
Clean coding-practices
 
Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8Clean Lambdas & Streams in Java8
Clean Lambdas & Streams in Java8
 
Functional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupFunctional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User Group
 
Clean Code - The Next Chapter
Clean Code - The Next ChapterClean Code - The Next Chapter
Clean Code - The Next Chapter
 
Clean Code
Clean CodeClean Code
Clean Code
 
Clean code
Clean codeClean code
Clean code
 
Clean code
Clean codeClean code
Clean code
 
Introduction to Rust language programming
Introduction to Rust language programmingIntroduction to Rust language programming
Introduction to Rust language programming
 
Vert.x for Microservices Architecture
Vert.x for Microservices ArchitectureVert.x for Microservices Architecture
Vert.x for Microservices Architecture
 
Railway Oriented Programming
Railway Oriented ProgrammingRailway Oriented Programming
Railway Oriented Programming
 
Real Life Clean Architecture
Real Life Clean ArchitectureReal Life Clean Architecture
Real Life Clean Architecture
 
Clean Code
Clean CodeClean Code
Clean Code
 

Similaire à The tests are trying to tell you something@VoxxedBucharest.pptx

Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxVictor Rentea
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019Paulo Clavijo
 
Unit testing - 9 design hints
Unit testing - 9 design hintsUnit testing - 9 design hints
Unit testing - 9 design hintsVictor Rentea
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Groupbrada
 
System verilog important
System verilog importantSystem verilog important
System verilog importantelumalai7
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestSeb Rose
 
Testing Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdfTesting Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdfVictor Rentea
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.Matt Eland
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The EnterpriseDaniel Egan
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesFab L
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Codeerikmsp
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9google
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0Michael Vorburger
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Steven Smith
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxdanhaley45372
 
Java programming concept
Java programming conceptJava programming concept
Java programming conceptSanjay Gunjal
 
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...DevSecCon
 

Similaire à The tests are trying to tell you something@VoxxedBucharest.pptx (20)

Test-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptxTest-Driven Design Insights@DevoxxBE 2023.pptx
Test-Driven Design Insights@DevoxxBE 2023.pptx
 
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019Breaking Dependencies Legacy Code -  Cork Software Crafters - September 2019
Breaking Dependencies Legacy Code - Cork Software Crafters - September 2019
 
Unit testing - 9 design hints
Unit testing - 9 design hintsUnit testing - 9 design hints
Unit testing - 9 design hints
 
Framework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users GroupFramework Design Guidelines For Brussels Users Group
Framework Design Guidelines For Brussels Users Group
 
System verilog important
System verilog importantSystem verilog important
System verilog important
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Testing Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdfTesting Microservices @DevoxxBE 23.pdf
Testing Microservices @DevoxxBE 23.pdf
 
Mini-Training: Javascript Patterns
Mini-Training: Javascript PatternsMini-Training: Javascript Patterns
Mini-Training: Javascript Patterns
 
How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.How do you tame a big ball of mud? One test at a time.
How do you tame a big ball of mud? One test at a time.
 
Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
 
Linq To The Enterprise
Linq To The EnterpriseLinq To The Enterprise
Linq To The Enterprise
 
DevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation SlidesDevSecCon SG 2018 Fabian Presentation Slides
DevSecCon SG 2018 Fabian Presentation Slides
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
 
Linq 1224887336792847 9
Linq 1224887336792847 9Linq 1224887336792847 9
Linq 1224887336792847 9
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
Design Pattern Mastery - Momentum Dev Con 19 Apr 2018
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
Testing w-mocks
Testing w-mocksTesting w-mocks
Testing w-mocks
 
Java programming concept
Java programming conceptJava programming concept
Java programming concept
 
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...DevSecCon Singapore 2018 -  Remove developers’ shameful secrets or simply rem...
DevSecCon Singapore 2018 - Remove developers’ shameful secrets or simply rem...
 

Plus de Victor Rentea

Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24Victor Rentea
 
Distributed Consistency.pdf
Distributed Consistency.pdfDistributed Consistency.pdf
Distributed Consistency.pdfVictor Rentea
 
From Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptxFrom Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptxVictor Rentea
 
Profiling your Java Application
Profiling your Java ApplicationProfiling your Java Application
Profiling your Java ApplicationVictor Rentea
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipVictor Rentea
 
Refactoring blockers and code smells @jNation 2021
Refactoring   blockers and code smells @jNation 2021Refactoring   blockers and code smells @jNation 2021
Refactoring blockers and code smells @jNation 2021Victor Rentea
 
Hibernate and Spring - Unleash the Magic
Hibernate and Spring - Unleash the MagicHibernate and Spring - Unleash the Magic
Hibernate and Spring - Unleash the MagicVictor Rentea
 
Integration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzIntegration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzVictor Rentea
 
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021Victor Rentea
 
Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Victor Rentea
 
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...Victor Rentea
 
Pure Functions and Immutable Objects
Pure Functions and Immutable ObjectsPure Functions and Immutable Objects
Pure Functions and Immutable ObjectsVictor Rentea
 
Definitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaDefinitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaVictor Rentea
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipVictor Rentea
 
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...Victor Rentea
 

Plus de Victor Rentea (17)

Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24Microservice Resilience Patterns @VoxxedCern'24
Microservice Resilience Patterns @VoxxedCern'24
 
Distributed Consistency.pdf
Distributed Consistency.pdfDistributed Consistency.pdf
Distributed Consistency.pdf
 
From Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptxFrom Web to Flux @DevoxxBE 2023.pptx
From Web to Flux @DevoxxBE 2023.pptx
 
Profiling your Java Application
Profiling your Java ApplicationProfiling your Java Application
Profiling your Java Application
 
OAuth in the Wild
OAuth in the WildOAuth in the Wild
OAuth in the Wild
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software Craftsmanship
 
Refactoring blockers and code smells @jNation 2021
Refactoring   blockers and code smells @jNation 2021Refactoring   blockers and code smells @jNation 2021
Refactoring blockers and code smells @jNation 2021
 
Hibernate and Spring - Unleash the Magic
Hibernate and Spring - Unleash the MagicHibernate and Spring - Unleash the Magic
Hibernate and Spring - Unleash the Magic
 
Integration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzIntegration testing with spring @JAX Mainz
Integration testing with spring @JAX Mainz
 
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
The Proxy Fairy and the Magic of Spring @JAX Mainz 2021
 
Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021Pure functions and immutable objects @dev nexus 2021
Pure functions and immutable objects @dev nexus 2021
 
TDD Mantra
TDD MantraTDD Mantra
TDD Mantra
 
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
Definitive Guide to Working With Exceptions in Java - takj at Java Champions ...
 
Pure Functions and Immutable Objects
Pure Functions and Immutable ObjectsPure Functions and Immutable Objects
Pure Functions and Immutable Objects
 
Definitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaDefinitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in Java
 
Extreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software CraftsmanshipExtreme Professionalism - Software Craftsmanship
Extreme Professionalism - Software Craftsmanship
 
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
Engaging Isolation - What I've Learned Delivering 250 Webinar Hours during CO...
 

Dernier

Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Intelisync
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Dernier (20)

Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)Introduction to Decentralized Applications (dApps)
Introduction to Decentralized Applications (dApps)
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

The tests are trying to tell you something@VoxxedBucharest.pptx

  • 1. Your tests are trying to tell you something ... 10 design hints you were missing Read the article: https://victorrentea.ro/blog/design-insights-from-unit-testing/ About the speaker: https://victorrentea.ro
  • 2. victorrentea.ro/training-offer Hi, I'm Victor Rentea 🇷🇴 Java Champion, 17 years of code, code, code, code, code.... Consultant & Trainer: 5000 developers of 100+ companies in EU: ❤️ Clean Code, Architecture, Unit Testing 🛠 Spring Framework, Hibernate/JPA, Reactive/WebFlux ⚡️ Java Performance, Secure Coding 🔐 Conference Speaker – find many recoded talks online Founder of Bucharest Software Crafters Community: 5000+ members 🔥 Free monthly Zoom webinars, 1-2 hours after work. Join at victorrentea.ro/community Past events on my channel: youtube.com/vrentea Father of 👧👦, woke up at night by a 😺: VictorRentea.ro
  • 3. 3 VictorRentea.ro From the Agile Ideology ... Emergent Design while we keep shipping shit working software fast, the design of the system will naturally evolve by itself (as opposed to large up-front design that can cause overengineering)
  • 4. 4 VictorRentea.ro Writing Tests give you hints on when to improve the design Emergent Design that never emerged 😩 We need triggers! When should I refine the design?
  • 5. 5 VictorRentea.ro Kent Beck Creator of Extreme Programming (XP) a very technical form of Agile Inventor of TDD Author of JUnit Father of Unit Testing
  • 6. 6 VictorRentea.ro 1. Passes all Tests 💪 2. Expresses Intent = SRP, Domain Names 2. No Duplication = DRY🌵 3. Keep it Simple = KISS💋 Rules of Simple Design by Kent Beck https://martinfowler.com/bliki/BeckDesignRules.html  design feedback 💎
  • 7. 7 VictorRentea.ro Testing in many Successful Monoliths : Ice-Cream Cone Testing Anti-Pattern 30 manual testers 🧔🏼♂️🧔🏼♂️🧔🏼♂️🧔🏼♂️ 🧔🏼♂️🧔🏼♂️🧔🏼 20% coverage (GUI robots, Selenium,..) absent (old frameworks) scarce, 10% coverage
  • 8. 8 VictorRentea.ro https://martinfowler.com/articles/practical-test-pyramid.html E2E More design feedback Testing Pyramid Test a thin slice of behavior MOCKS Test a group of modules Test everything as whole ⭐️ Deep Edge Case ⭐️ Critical Business Flow (eg. checkout) More busine$$ safety overlapping is normal to happen
  • 10. 10 VictorRentea.ro Why we 💖 Mocks Isolated Tests from external systems 🤔 Fast 🐇 no framework, DB, external APIs Simpler when testing high complexity 😵💫  Alternatives: in-mem DB Testcontainers 🐳 WireMock Cheating* * James Coplien in https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf
  • 11. 11 VictorRentea.ro Logic under test Cyclomatic Complexity = number of independent execution paths through code ≈ max no. tests Test Test Test Test Test Test Test CC=5 CC=6 f(a) g(b) calls f() calling g() together have a CC = Many To cover all branches Heavy setup and input data Tests for a+b become... 3 0
  • 13. 13 VictorRentea.ro Why we 🤔 Mocks Uncaught Bugs 😱 despite 1000s of green✅ tests: lock, tap, doors, umbrella 🧔 Fragile Tests 💔 that break when refactoring internals Unreadable Tests 😵💫 A test using 5+ mocks 
  • 15. 15 VictorRentea.ro The test has 20 lines full of mocks 😵💫 😡 BURN THE TEST! has a bad cost/benefit ratio HONEYCOMB TESTING Outside-in from integration- to unit- test WHAT AM I TESTING HERE ? syndrome Tested prod code has 4 lines 😩 f(..) { a = api.fetchB(repoA.find(..).getBId()); d = service.createD(a,b,repoC.find(..)); repo3.save(d); mq.send(d.id); } REDESIGN PRODUCTION Collapse Middle-Man / useless layer
  • 17. 17 VictorRentea.ro The Bad Monolith ("Big ball of mud") has - Highly coupled code - Huge complexity behind a few entry points  the need for lots of Unit Tests  Microservices / Modulith - Huge complexity in a single microservice = bad practice  Break µservice/module - Many APIs, hiding a decent amount of complexity (🙏) - Easier to test more at the API level  Honeycomb Testing Strategy Testing Microservices is different
  • 18. 18 VictorRentea.ro Integration test the entire microservice in isolation Integrated may fail because of another system Implementation Detail complex parts of the code Start up all microservices in the ecosystem. Expensive, flaky, but business-critical e2e tests (eg checkout) Responsibility of a central QA team(?) Test end-to-end as many flows in your system. Starts up the entire system (@SpringBootTest)  Keep tests isolated without mocks Separate tests for the parts of the code naturally isolated with high internal complexity. Mocks are allowed Still: test roles, not methods/classes Write social unit tests Honeycomb Testing Strategy (suitable for microservices) Testcontainers 🐳 WireMock DB ES Kafka ... API many tests on one entrypoint decouple and test alone https://engineering.atspotify.com/2018/01/testing-of-microservices/
  • 19. 19 VictorRentea.ro ⚠️ Test manageable complexity without mocks A B Instead, test "components" (groups of objects) #0 Internal refactoring won't break tests
  • 20. 20 VictorRentea.ro You get most design feedback from tests in 'Implementation Detail' Implementation Detail
  • 21. 21 VictorRentea.ro Unit Testing puts more design pressure on methods and data structures #1
  • 22. 22 VictorRentea.ro var bigObj = new BigObj(); bigObj.setA(a); bugObj.setB(b); prod.method(bigObj); Tests must create bigObj just to pass two inputs🧔 method(bigObj) MUTABLE DATA in 2023? using only 2 of the 15 fields in bigObj method(a, b) Precise Signatures prod.method(a, b); Also, simpler tests: Pass only necessary data to functions ✅ when(bigObj.getPart1()) .thenReturn(p1); ⛔️ Don't Mock Getters ⛔️ Mock behavior, not data prod.method(new AB(a, b)); method(ab) Parameter Object Testing highly complex logic: ⛔️ Don't have Mocks return Mocks⛔️
  • 23. 23 VictorRentea.ro 🏰 Constrained Objects = data structures that guard their internal consistency by throwing exceptions, (eg required fields, string size / regex, or more complex domain rules)  Mutable (eg Domain Entities, Aggregates) via constructor and setters/mutator methods  Immutable❤️ (Value Objects) via constructor
  • 24. 24 VictorRentea.ro A group of classes has a clear role and manageable complexity (A) An Object is Constrained ✅ but Large (B) Object Mother Pattern* eg. TestData.john(): Customer coupling Break Domain Entities in separate Bounded Contexts packages > modulith > microservices  Test the entire group with a social unit-test✅: requires larger setup and larger input * https://martinfowler.com/bliki/ObjectMother.html (2006) Same test data factory used in different verticals customer | order ↓ Break Object Mother per vertical CustomerTestData | OrderTestData Creating valid test data gets cumbersome CREEPY A shared class creating valid test objects
  • 25. 26 VictorRentea.ro Your complex logic directly uses APIs or heavy libraries: Unit-testing your logic requires understanding the semantics of: The External API: to populate/assert DTOs The Library: to mock it ... api.call(apiDetails); ... dto.getStrangeField() ... Lib.use(mysteriousParam,...) Unit Tests speak your Domain Model Unit Tests are first-class citizens of your project #respect them Agnostic Domain Isolate complex logic from the outside world ... clientAdapter.call(domainStuff) ... domainObject.getMyField() ... libAdapter.use(😊)
  • 26. application / infra Value Object Entity id Domain Service Domain Service agnostic domain My DTOs External API External DTOs Clien t External Systems Façade Controller Repo IAdapter Adapter ⛔️ ⛔️ Deep complexity is kept inside for easier testing Simplified Onion Architecture Interf Ugly Invasive Library
  • 27. 28 VictorRentea.ro #1 Unit Testing loves precise signatures smaller data structures agnostic domain
  • 28. 29 VictorRentea.ro #2 Unit Testing puts design pressure on Highly Complex Logic 🤔
  • 29. 30 VictorRentea.ro class Big { f() { //complex g(); } g() { //complex } } Inside the same class, a complex function f() calls a complex g() g() is complex => unit-tested separately When testing f(), can I avoid executing g()? That is: can I mock a local method call? class HighLevel { LowLevel low; f() {//complex low.g(); } } class LowLevel { g() {/*complex*/} } ↓ Partial Mock (@Spy) Hard to maintain tests: Which method is real, which is mocked?🧔 Split by Layers of Abstraction (high-level policy vs low-level details) Tolerable testing Legacy Code If splitting the class doesn't feel right, test f+g together with bigger tests
  • 30. 31 VictorRentea.ro class HighLevel { LowLevel low; f() {//complex low.g(); } } Split by Layers of Abstraction = vertical split of a class class LowLevel { g() {/*complex*/} }
  • 31. 32 VictorRentea.ro class Wide { A a; B b; //+more dependencies complexA() {..a.fa()..} complexB() {..b.fb()..} } @ExtendWith(MockitoExtension) class WideTest { @Mock A a; @Mock B b; @InjectMocks Wide wide; // 5 tests for complexA() // 4 tests for complexB() } ↓ Separate test classes: ComplexATest, ..B.. (keep before useful for all tests 👌) Complex methods in the same class use different sets of dependencies: class ComplexA { A a; // +more complexA() { ..a.fa().. } } class ComplexB { B b; // +more complexB() { ..b.fb().. } } @BeforeEach void fixture() { when(a.fa()).then... when(b.fb()).then... } Split Unrelated Complexity not used when testing complex1() what part of the before is used by my failed test? ** Mockito (since v2.0) throws UnnecessaryStubbingException if a when..then is not used by a @Test, when using MockitoExtension ** This is BIG 🧔 FIXTURE CREEP test setup DRY Principle
  • 32. 33 VictorRentea.ro Split Unrelated Complexity = horizontal split a class class ComplexA { A a; // +more complexA() { ..a.fa().. } } class ComplexB { B b; // +more complexB() { ..b.fb().. } }
  • 33. 34 VictorRentea.ro vertically ↕️ Tests give us hints on how to break complexity horizontally ↔️ When should we follow those hints?
  • 34. 35 VictorRentea.ro Mock Roles, not Objects http://jmock.org/oopsla2004.pdf The dawn of Mocks (2004):
  • 35. 36 VictorRentea.ro The dawn of Mocks (2004):
  • 36. 37 VictorRentea.ro BAD HABIT Mock Roles, not Objects http://jmock.org/oopsla2004.pdf You implement a new feature > ((click in UI/postman)) > It works > Yee-Haa! OMG, I forgot about unit-tests 😱 ...then you write unit tests by mocking all dependencies of the prod class you wrote Several years later, you complain that your tests are fragile and impede refactoring Contract-Driven Design Before mocking a dependency, clarify its responsibility = Changing an API you mocked is painful 😭
  • 37. 38 VictorRentea.ro "Unit Testing means mocking all dependencies of a class" - common belief "It's perfectly fine for unit tests to talk to databases and filesystems!"- Ian Cooper Unit Testing = ?
  • 38. 39 VictorRentea.ro timeframe for developing your feature When do you start writing tests? ✅ understand the problem => early questions to biz ✅ early design feedback 💎 💎 💎 ✅ real test coverage => courage to refactor
  • 39. 40 VictorRentea.ro #2 Unit Testing help us break complexity: Horizontally Vertically By Roles
  • 40. 41 VictorRentea.ro #3 Unit Testing promotes Functional Programming pure functions & immutable objects
  • 41. 42 VictorRentea.ro 1) Has no Side-Effects (doesn't change anything) INSERT, POST, send message, field changes, files 2) Same Inputs => Same Output (no external source of data) GET, SELECT, current time, random, … Pure Function aka "Referential Transparency"
  • 42. 43 VictorRentea.ro No Network or files No Changes to Data No time/random Pure Functions use immutable objects❤️ they are super fast (simplified definition)
  • 43. 44 VictorRentea.ro a = repo1.findById(..) b = repo2.findById(..) c = api.call(..) 🤯complexity🤯 repo3.save(d); mq.send(d.id); Very complex logic using many dependencies (eg: computePrice, applyDiscounts) Many tests using heavy mocking when(..).thenReturn(a); when(..).thenReturn(b); when(..).thenReturn(c); prod.complexAndCoupled(); verify(..).save(captor); verify(..).send(...); x15=😖 Easier to test w/ less mocks ✅ d = prod.pure(a,b,c); assertThat(d)... Reduce Coupling of Complex Logic D pure(a,b,c) { 🤯complexity🤯 return d; } Easier to understand ✅
  • 44. 45 © VictorRentea.ro a training by Complexity Functional Core Imperative Shell / Functional Core Segregation State Mutation DB Imperative Shell API call Files Complex Logic
  • 45. 46 © VictorRentea.ro a training by Functional Core Imperative Shell Imperative Shell / Functional Core Segregation
  • 46. 47 VictorRentea.ro method(Mutable order, d) { ds.applyDiscounts(order, d); var price = cs.computePrice(order); return price; } ... but you use mutable objects Swapping two lines can still cause bugs that is: 4000 ✅ tests, ❌ bugs in production You have 4.000 unit tests, 100% test coverage 😲 👏 ↓ Paranoid Testing (verifying method call order) Immutable Objects method(Immutable order, d) { var discountedOrder = ds.applyDiscounts(order, d); var price = cs.computePrice(discountedOrder); return price; } TEMPORAL COUPLING Swapping two lines ❌ does not compile
  • 47. 48 VictorRentea.ro 1. Collapse Middle-Man vs "What am I testing here?" Syndrome 2. Honeycomb Testing Strategy vs Fragile microscopic Unit Tests 3. Precise Signatures 4. Tailored Data Structures vs Creepy Object Mother 5. Keep complexity inside Agnostic Domain not on APIs or Extensive Libraries 6. Separate Complexity by Layers of Abstraction ↕️ vs @Spy 7. Separate Unrelated Complexity ↔️ vs Fixture Creep (bloated setup) 8. Refine Roles (Mock Roles, not Objects) vs blindly @Mock all dependencies 9. More Complexity => Less Dependencies vs Mock-full tests 10.Promote Immutable Objects vs Temporal Coupling Design Hints from Tests
  • 49. Writing fine-grained unit early increases friction with bad design
  • 50. 51 VictorRentea.ro Unit Testing Reading Guide 1] Classic TDD⭐️⭐️⭐️ (mock-less) https://www.amazon.com/Test-Driven-Development-Kent-Beck/dp/0321146530 Mock Roles, not Objects ⭐️⭐️⭐️: http://jmock.org/oopsla2004.pdf "Is TDD Dead?" https://martinfowler.com/articles/is-tdd-dead/ Why Most Unit Testing is Waste (James Coplien): https://rbcs-us.com/documents/Why-Most-Unit-Testing-is-Waste.pdf vs Integrated Tests are a Scam(J Brains): https://blog.thecodewhisperer.com/permalink/integrated-tests-are-a-scam 2] London TDD⭐️⭐️⭐️ (mockist) https://www.amazon.com/Growing-Object-Oriented-Software-Guided-Tests/dp/0321503627 3] Patterns⭐️ https://www.amazon.com/Art-Unit-Testing-examples/dp/1617290890 4] https://www.amazon.com/xUnit-Test-Patterns-Refactoring-Code/dp/0131495054/ 5] (skip through) https://www.amazon.com/Unit-Testing-Principles-Practices-Patterns
  • 51. 52 VictorRentea.ro Write more automated tests, earlier, to have time to listen to what tests are trying to tell you Join my community to keep in touch, FOR free monthly, ONLINE debates : victorrentea.ro

Notes de l'éditeur

  1. I'm victor rentea, I'm a java champion of Romania, working in our field for 17 years. 8 years ago I realized coding was not enough for me, and I started looking around to help the others. Today this is my full-time job: training and consultancy for companies throughout Europe. My favorite topics are ... but of course, to talk about these topics you have to master the frameworks you use, so I do intense workshops on Spring Framework, .... More senior groups often call me for performance tuning or secure coding. If you want to know more, you can find there my full training offer Besides the talks at different conferences that you can find online, I try to to one webinar each month for my community. A few years ago I started this group on meetup to have where to share my ideas and learn from the others in turn. - This community has exceeded my wildest dreams, turning into one of the largest communities in the world on Software Craftsmanship. - So what happens there? One day a month we have a zoom online webinar of 1-2 hours after work, during which we discuss one topic and then debate ideas from the participants – usually we have close to 100 live participants, so it's very engaging. If you want to be part of the fun, DO join us, it's completely free. - Many past events are available on my youtube channel. - Outside of work, I have 2 kids and a cat that wakes us up in the middle of the night.