SlideShare a Scribd company logo
1 of 27
Download to read offline
Bigger Unit Test Are Better
why having many small unit test classes will slow you down
%: whoami
• Peter Schuler
• Java developer @ Rabobank @ Tribe Wonen
• Trainer and coach
• Writing code since 1992
• Java since 1998
• Interests: Clean code, TDD, Code quality, Security, Architecture,
Gardening
• Lives in the Netherlands / married / three daughters / three dogs
%: whoami
• Peter Schuler
• Java developer / Architect @ Rabobank
• Trainer and coach
• Writing code since 1992
• Java since 1998
• Interests: Clean code, TDD, Code quality, Security, Architecture,
Gardening
• Lives in the Netherlands / married / three daughters / three dogs
I am also 2,04 meters. J
Agenda
• What do I mean with bigger?
• Why do we write unit tests?
• Why should they be bigger?
• How do you make them bigger?
• How big is big enough?
Unit test should have a bigger test scope
So we go from this:
Class
Unit Test
Unit Test Class
Class
Class
Class
To this:
Why unit test?
You tell me!
Join the word could:
livecloud.online/join/U446
Why unit test?
I was forced to by some ‘senior’ developer
Apparently we need coverage
If I don’t the build won’t pass
Is my code working?
Is my code STILL working?
Will this change affect other parts of the code?
Dare I make this change to the code?
Do I trust my code?
Do I trust code?
Why a bigger testscope?
Which design is better?
SubElement1MapperTest
The problem with being small
Response mapper
Sub Element 1 mapper
Sub Element 2 mapper
Enum 2 mapper
ResponseMapperTest
SubElement2MapperTest
Enum2MapperTest
The problem with being small
• Tightly coupled to the coding structure eg:
• When your code structure changes, your test structure will change
• This will make refactoring a hell
• So ..when you need them the most
…. small tests will actually work against you.
• The test cannot be translated to business concepts.
• You test classes … but not their interaction …
Let’s look at small scoped tests
• Especially classes at both ends of the hierarchy look silly.
• Can you discuss these tests with the business?
• Can you map them on a functional requirement?
https://martinfowler.com/articles/mocksArentStubs.html
Martin Fowler
Let’s do a little though experiment
• Let’s assume that these are
classes / components.
• How will we test that they do as
designed using mocks?
• Shoot!
https://youtu.be/IKwWu2w1gGk
Bigger scope is better
High level interface / service /
rest controller / sub-part
Sub Element 1 mapper
Sub Element 2 mapper
Enum 2 mapper
Unit Test
DAO
DB
TestDataGenerators
Test Infa Setup
MatcherHelpers
External JAR
Let’s look at bigger scoped tests
• So now we have tests that we can explain to the business.
• Let’s look at how to make them.
Hurdles when being bigger scoped
• They need more “plumbing code” to get set up.
• They need more asserts and more setup.
• Testing every small aspect might be to hard.
• They fail when the business requirements change.
Wait… that is actually a good thing!
Couple test to behaviour not stucture
https://www.facebook.com/notes/kent-beck/unit-tests/1726369154062608/
https://youtube.com/playlist?list=PLlmVY7qtgT_lkbrk9iZNizp978mVzpBKl
How to make them bigger - Plumbing
• Use Spring..
How to make them bigger - Plumbing
• Or just be your own dependency injection framework:
• Or perhaps use Google Guice
• But when you are using Spring, Spring will mostly do just fine.
How to make them bigger – Test Data
• Use TestBuilders to build Bigger Object Quicker
How to make them bigger – Test Data
• Chain test builders for better semantics. Go to town..
• Grow your own specific test semantics / DSL.
aRequest()
.with()
.twoApplications()
.existingMortgages()
.add(anAnuity())
.add(new CreditMortgageBuilder()
.setSaldo(20000)
.setLimit(40000))
.newHouse(450000)
.currentHouse(200000)
.build();
How to make them bigger – Asserts
• Bigger scope means more asserts.
• This drawback is harder to mitigate.
• Solutions:
• Apply clean code standards.
• Test only the small aspects of your who set.
• Make helper functions and delegate
• Split up your tests in multiple test classes for different aspects.
• I once split a calculation test class in positive, negative and weird cases.
How to make them bigger – Test all
• Bigger scope makes testing every possible mutation harder.
• Solution:
• Do not test everything BIG.
• It is still fine to write specific tests for specific classes.
• But please try to test against stable interfaces.
• And you can still test small aspects with a big test
Be Big and Small at the same time
• You can still be Big, but test a small aspect of your code.
• This also helps with having too many asserts.
How big should a unit test scope be?
• In scope:
• Everything that is a “pure function”
• Layers in your application.
• External JAR files
• Controllers
• DB’s -> can be unit tested easily these days.
• Out of scope:
• External network calls.
• But this is all a guideline … you are in control of your own fate J
What I want you to do
• Make your test scope bigger.
• Stop excessive mocking!
• Base your unit test on business data.
• But … do not ..
• … stop writing tests
• … stop feeling free to test small
• … only think big.

More Related Content

Similar to Bigger Unit Test Are Better

An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
Steven Casey
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
Einar Ingebrigtsen
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
MaslowB
 
Becoming a better programmer - unit testing
Becoming a better programmer - unit testingBecoming a better programmer - unit testing
Becoming a better programmer - unit testing
Duy Tan Geek
 

Similar to Bigger Unit Test Are Better (20)

Test Driven Development with Laravel
Test Driven Development with LaravelTest Driven Development with Laravel
Test Driven Development with Laravel
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
Practical TDD Demonstrated
Practical TDD DemonstratedPractical TDD Demonstrated
Practical TDD Demonstrated
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Unit testing
Unit testingUnit testing
Unit testing
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Completely Test-Driven
Completely Test-DrivenCompletely Test-Driven
Completely Test-Driven
 
Clean code
Clean codeClean code
Clean code
 
Tdd
TddTdd
Tdd
 
Test Driven Development - a Practitioner’s Perspective
Test Driven Development - a Practitioner’s PerspectiveTest Driven Development - a Practitioner’s Perspective
Test Driven Development - a Practitioner’s Perspective
 
Becoming a better programmer - unit testing
Becoming a better programmer - unit testingBecoming a better programmer - unit testing
Becoming a better programmer - unit testing
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 

Recently uploaded

AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
mbmh111980
 

Recently uploaded (20)

COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purityAPVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data Migration
 
AI Hackathon.pptx
AI                        Hackathon.pptxAI                        Hackathon.pptx
AI Hackathon.pptx
 
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdfMicrosoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
Microsoft 365 Copilot; An AI tool changing the world of work _PDF.pdf
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand5 Reasons Driving Warehouse Management Systems Demand
5 Reasons Driving Warehouse Management Systems Demand
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion Production
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdf
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 

Bigger Unit Test Are Better

  • 1. Bigger Unit Test Are Better why having many small unit test classes will slow you down
  • 2. %: whoami • Peter Schuler • Java developer @ Rabobank @ Tribe Wonen • Trainer and coach • Writing code since 1992 • Java since 1998 • Interests: Clean code, TDD, Code quality, Security, Architecture, Gardening • Lives in the Netherlands / married / three daughters / three dogs
  • 3. %: whoami • Peter Schuler • Java developer / Architect @ Rabobank • Trainer and coach • Writing code since 1992 • Java since 1998 • Interests: Clean code, TDD, Code quality, Security, Architecture, Gardening • Lives in the Netherlands / married / three daughters / three dogs I am also 2,04 meters. J
  • 4. Agenda • What do I mean with bigger? • Why do we write unit tests? • Why should they be bigger? • How do you make them bigger? • How big is big enough?
  • 5. Unit test should have a bigger test scope So we go from this: Class Unit Test Unit Test Class Class Class Class To this:
  • 6. Why unit test? You tell me! Join the word could: livecloud.online/join/U446
  • 7. Why unit test? I was forced to by some ‘senior’ developer Apparently we need coverage If I don’t the build won’t pass Is my code working? Is my code STILL working? Will this change affect other parts of the code? Dare I make this change to the code? Do I trust my code? Do I trust code?
  • 8. Why a bigger testscope? Which design is better?
  • 9. SubElement1MapperTest The problem with being small Response mapper Sub Element 1 mapper Sub Element 2 mapper Enum 2 mapper ResponseMapperTest SubElement2MapperTest Enum2MapperTest
  • 10. The problem with being small • Tightly coupled to the coding structure eg: • When your code structure changes, your test structure will change • This will make refactoring a hell • So ..when you need them the most …. small tests will actually work against you. • The test cannot be translated to business concepts. • You test classes … but not their interaction …
  • 11. Let’s look at small scoped tests • Especially classes at both ends of the hierarchy look silly. • Can you discuss these tests with the business? • Can you map them on a functional requirement?
  • 13. Let’s do a little though experiment • Let’s assume that these are classes / components. • How will we test that they do as designed using mocks? • Shoot!
  • 15. Bigger scope is better High level interface / service / rest controller / sub-part Sub Element 1 mapper Sub Element 2 mapper Enum 2 mapper Unit Test DAO DB TestDataGenerators Test Infa Setup MatcherHelpers External JAR
  • 16. Let’s look at bigger scoped tests • So now we have tests that we can explain to the business. • Let’s look at how to make them.
  • 17. Hurdles when being bigger scoped • They need more “plumbing code” to get set up. • They need more asserts and more setup. • Testing every small aspect might be to hard. • They fail when the business requirements change. Wait… that is actually a good thing!
  • 18. Couple test to behaviour not stucture https://www.facebook.com/notes/kent-beck/unit-tests/1726369154062608/ https://youtube.com/playlist?list=PLlmVY7qtgT_lkbrk9iZNizp978mVzpBKl
  • 19. How to make them bigger - Plumbing • Use Spring..
  • 20. How to make them bigger - Plumbing • Or just be your own dependency injection framework: • Or perhaps use Google Guice • But when you are using Spring, Spring will mostly do just fine.
  • 21. How to make them bigger – Test Data • Use TestBuilders to build Bigger Object Quicker
  • 22. How to make them bigger – Test Data • Chain test builders for better semantics. Go to town.. • Grow your own specific test semantics / DSL. aRequest() .with() .twoApplications() .existingMortgages() .add(anAnuity()) .add(new CreditMortgageBuilder() .setSaldo(20000) .setLimit(40000)) .newHouse(450000) .currentHouse(200000) .build();
  • 23. How to make them bigger – Asserts • Bigger scope means more asserts. • This drawback is harder to mitigate. • Solutions: • Apply clean code standards. • Test only the small aspects of your who set. • Make helper functions and delegate • Split up your tests in multiple test classes for different aspects. • I once split a calculation test class in positive, negative and weird cases.
  • 24. How to make them bigger – Test all • Bigger scope makes testing every possible mutation harder. • Solution: • Do not test everything BIG. • It is still fine to write specific tests for specific classes. • But please try to test against stable interfaces. • And you can still test small aspects with a big test
  • 25. Be Big and Small at the same time • You can still be Big, but test a small aspect of your code. • This also helps with having too many asserts.
  • 26. How big should a unit test scope be? • In scope: • Everything that is a “pure function” • Layers in your application. • External JAR files • Controllers • DB’s -> can be unit tested easily these days. • Out of scope: • External network calls. • But this is all a guideline … you are in control of your own fate J
  • 27. What I want you to do • Make your test scope bigger. • Stop excessive mocking! • Base your unit test on business data. • But … do not .. • … stop writing tests • … stop feeling free to test small • … only think big.