Facebook Pixel
Searching...
English
EnglishEnglish
EspañolSpanish
简体中文Chinese
FrançaisFrench
DeutschGerman
日本語Japanese
PortuguêsPortuguese
ItalianoItalian
한국어Korean
РусскийRussian
NederlandsDutch
العربيةArabic
PolskiPolish
हिन्दीHindi
Tiếng ViệtVietnamese
SvenskaSwedish
ΕλληνικάGreek
TürkçeTurkish
ไทยThai
ČeštinaCzech
RomânăRomanian
MagyarHungarian
УкраїнськаUkrainian
Bahasa IndonesiaIndonesian
DanskDanish
SuomiFinnish
БългарскиBulgarian
עבריתHebrew
NorskNorwegian
HrvatskiCroatian
CatalàCatalan
SlovenčinaSlovak
LietuviųLithuanian
SlovenščinaSlovenian
СрпскиSerbian
EestiEstonian
LatviešuLatvian
فارسیPersian
മലയാളംMalayalam
தமிழ்Tamil
اردوUrdu
Effective Unit Testing

Effective Unit Testing

A guide for Java developers
by Lasse Koskela 2013 248 pages
3.79
100+ ratings
Listen

Key Takeaways

1. Unit tests are essential for maintaining code quality and developer productivity

Tests help us catch mistakes.

Tests provide value by acting as a safety net against regression and aiding in design. They allow developers to confidently make changes and refactor code, knowing that any unintended side effects will be caught. Good tests also serve as documentation, helping developers understand the intended behavior of the code.

The feedback loop provided by tests is crucial for maintaining productivity. Quick feedback allows developers to catch and fix issues early, before they become more complex and time-consuming to resolve. This is why having a fast-running test suite is important - it encourages developers to run tests frequently throughout the development process.

2. Good tests are readable, maintainable, and trustworthy

A test should have only one reason to fail.

Readability is crucial for test code. Tests should clearly communicate their intent and be easy to understand at a glance. This often means using descriptive test names, avoiding complex logic within tests, and structuring tests in a consistent manner (e.g., using the Arrange-Act-Assert pattern).

Maintainability ensures that tests remain valuable as the codebase evolves. This involves avoiding duplication, keeping tests focused on a single behavior, and using appropriate levels of abstraction. Trustworthy tests are reliable and consistent in their results, avoiding flakiness or false positives/negatives.

3. Test doubles enable isolation and faster execution of unit tests

Test doubles help us isolate the code under test so that we can simulate all scenarios and test all behaviors the code should exhibit.

Types of test doubles:

  • Stubs: Provide canned answers to calls
  • Fakes: Have working implementations, but use shortcuts unsuitable for production
  • Spies: Record information about how they were called
  • Mocks: Pre-programmed with expectations of calls they're expected to receive

Benefits of test doubles:

  • Isolate the code under test from its dependencies
  • Speed up test execution by avoiding slow operations (e.g., database calls, network requests)
  • Enable testing of edge cases and error scenarios
  • Allow testing of code that depends on external services or resources

4. Common test smells hinder readability and maintainability

Primitive assertions are unnecessary additions to your cognitive load.

Readability smells:

  • Primitive assertions: Using low-level comparisons instead of meaningful assertions
  • Hyperassertions: Overly specific assertions that break easily
  • Incidental details: Irrelevant information cluttering the test
  • Split personality: Tests trying to check multiple behaviors at once

Maintainability smells:

  • Duplication: Repeated code or data across tests
  • Conditional logic: Complex branching within tests
  • Flaky tests: Tests that fail intermittently
  • Overprotective tests: Unnecessary checks for intermediate states

Addressing these smells involves refactoring tests to be more focused, using appropriate levels of abstraction, and leveraging test framework features to improve clarity and reduce duplication.

5. Trustworthy tests avoid shallow promises and conditional logic

Tests are supposed to fail when they should.

Characteristics of trustworthy tests:

  • Clear intent: The test name and implementation align
  • Comprehensive assertions: Checking all relevant aspects of the behavior
  • Consistent results: The test always passes or fails for the same reasons
  • Independence: The test doesn't rely on external factors or other tests

Avoiding pitfalls:

  • Commented-out tests: Delete or fix them, don't leave them lingering
  • Never-failing tests: Ensure tests can actually fail when the behavior is incorrect
  • Conditional tests: Avoid branching logic within tests
  • Platform prejudice: Write tests that work across different environments

6. Testable design principles improve code modularity and ease of testing

Testability is not a term that describes whether software can be tested. It refers to software that is easily tested.

SOLID principles contribute to testable design:

  • Single Responsibility Principle: Classes and methods have one reason to change
  • Open-Closed Principle: Open for extension, closed for modification
  • Liskov Substitution Principle: Subtypes must be substitutable for their base types
  • Interface Segregation Principle: Many client-specific interfaces are better than one general-purpose interface
  • Dependency Inversion Principle: Depend on abstractions, not concretions

Guidelines for testable design:

  • Avoid complex private methods
  • Minimize use of static methods and singletons
  • Use dependency injection to allow for easy substitution of collaborators
  • Favor composition over inheritance
  • Wrap external libraries to control their impact on testability

7. Alternative JVM languages can enhance test expressiveness and conciseness

This is an exciting time to be a Java programmer.

Benefits of alternative JVM languages for testing:

  • More concise syntax (e.g., Groovy, Scala)
  • Powerful language constructs (e.g., closures, pattern matching)
  • Built-in support for creating test data and mocks
  • DSLs for behavior-driven development (BDD)

Examples:

  • Groovy: Simplified object creation, multiline strings, and optional semicolons
  • Spock Framework: Expressive specification language with built-in mocking support
  • Scala: Concise syntax and powerful functional programming features

Using alternative languages for tests can lead to more readable and maintainable test code, while still integrating seamlessly with Java production code.

8. Optimizing test execution speed is crucial for faster feedback loops

Delayed feedback causes trouble.

Strategies for speeding up tests:

  • Avoid unnecessary setup and teardown
  • Stay local: Minimize network calls and database access
  • Use in-memory databases for integration tests
  • Disable logging during test execution
  • Parallelize test execution

Build optimization techniques:

  • Use faster hardware (SSDs, more powerful CPUs)
  • Utilize RAM disks for I/O-intensive operations
  • Distribute builds across multiple machines
  • Leverage cloud computing resources for additional processing power

Faster test execution encourages developers to run tests more frequently, catching issues earlier in the development process and maintaining a smooth workflow.

Last updated:

Review Summary

3.79 out of 5
Average of 100+ ratings from Goodreads and Amazon.

Effective Unit Testing receives mostly positive reviews, with an average rating of 3.79 out of 5. Readers praise its comprehensive coverage of unit testing techniques, refactoring strategies, and best practices. Many find it valuable for beginners and intermediate developers, offering practical examples and insights. Some criticize the book for being too basic or lacking advanced content. Readers appreciate the author's clear writing style and the book's focus on improving test readability and maintainability. However, a few reviewers note that it may not be suitable for experienced developers or those seeking Java-specific guidance.

Your rating:

About the Author

Lasse Koskela is a software developer, trainer, and author known for his expertise in agile development practices and software testing. He has written several books on programming and software development, including "Test-Driven" and "Effective Unit Testing." Koskela is recognized for his ability to explain complex technical concepts in an accessible manner. He has presented at numerous conferences and workshops, sharing his knowledge on unit testing, test-driven development, and software design. With a background in Java development, Koskela has contributed to various open-source projects and is respected in the software development community for his practical insights and teaching approach.

Download PDF

To save this Effective Unit Testing summary for later, download the free PDF. You can print it out, or read offline at your convenience.
Download PDF
File size: 0.30 MB     Pages: 9

Download EPUB

To read this Effective Unit Testing summary on your e-reader device or app, download the free EPUB. The .epub digital book format is ideal for reading ebooks on phones, tablets, and e-readers.
Download EPUB
File size: 3.03 MB     Pages: 7
0:00
-0:00
1x
Dan
Andrew
Michelle
Lauren
Select Speed
1.0×
+
200 words per minute
Create a free account to unlock:
Bookmarks – save your favorite books
History – revisit books later
Ratings – rate books & see your ratings
Unlock unlimited listening
Your first week's on us!
Today: Get Instant Access
Listen to full summaries of 73,530 books. That's 12,000+ hours of audio!
Day 4: Trial Reminder
We'll send you a notification that your trial is ending soon.
Day 7: Your subscription begins
You'll be charged on Jan 3,
cancel anytime before.
Compare Features Free Pro
Read full text summaries
Summaries are free to read for everyone
Listen to summaries
12,000+ hours of audio
Unlimited Bookmarks
Free users are limited to 10
Unlimited History
Free users are limited to 10
What our users say
30,000+ readers
“...I can 10x the number of books I can read...”
“...exceptionally accurate, engaging, and beautifully presented...”
“...better than any amazon review when I'm making a book-buying decision...”
Save 62%
Yearly
$119.88 $44.99/yr
$3.75/mo
Monthly
$9.99/mo
Try Free & Unlock
7 days free, then $44.99/year. Cancel anytime.
Settings
Appearance
Black Friday Sale 🎉
$20 off Lifetime Access
$79.99 $59.99
Upgrade Now →