small medium large xlarge

Pragmatic Unit Testing in Java 8 with JUnit


Cover image for Pragmatic Unit Testing in Java 8 with JUnit

Pragmatic Unit Testing in Java 8 with JUnit


The Pragmatic Programmers classic is back! Freshly updated for modern software development, Pragmatic Unit Testing in Java 8 With JUnit teaches you how to write and run easily maintained unit tests in JUnit with confidence. You’ll learn mnemonics to help you know what tests to write, how to remember all the boundary conditions, and what the qualities of a good test are. You’ll see how unit tests can pay off by allowing you to keep your system code clean, and you’ll learn how to handle the stuff that seems too tough to test.

Customer Reviews

Langr, Hunt, and Thomas demonstrate, with abundant detailed examples, how
unit testing with JUnit works in the real world. Beyond just showing simple isolated
examples, they address the hard issues–things like mock objects, databases,
multithreading, and getting started with automated unit testing. Buy this book
and keep it on your desk, as you’ll want to refer to it often.

- Mike Cohn,

Author of "Succeeding with Agile, Agile Estimating and Planning," and "User Stories Applied"

Working from a realistic application, Jeff gives us the reasons behind unit testing,
the basics of using JUnit, and how to organize your tests. This is a super upgrade
to an already good book. If you have the original, you’ll find valuable new ideas
in this one. And if you don’t have the original, what’s holding you back?

- Ron Jeffries

Rational, balanced, and devoid of any of the typical religious wars surrounding
unit testing. Top-drawer stuff from Jeff Langr.

- Sam Rose

This book is an excellent resource for those new to the unit testing game. Experienced
developers should also at the very least get familiar with the very helpful

- Colin Yates,

Principal Architect,, QFI Consulting, LLP

See All Reviews

Choose Your Format(s)

  • $20.00 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

What You Need

You’ll need the Java SDK (Software Development Kit) version 8 or higher to work through the examples in the book. You’ll also want an IDE (Integrated Development Environment) in which to build code. While most of the book doesn’t assume use of any specific IDE, you’ll find a number of “getting started” screen shots to help you if you’re using Eclipse, IntelliJ IDEA, or NetBeans. Chances are good you’ll be working in one of those!

Contents & Extracts


  • Unit Testing Foundations
    • Building Your First JUnit Test excerpt
      • Reasons to Write a Unit Test
      • Learning JUnit Basics: Your First Passing Test
      • Arrange, Act, and Assert Your Way to a Test
      • Is the Test Really Testing Anything?
      • After
    • Getting Real with JUnit
      • Understanding What We’re Testing: The Profile Class
      • Determining What Tests We Can Write
      • Covering One Path
      • Tackling a Second Test
      • Initializing Tests with Before Methods
      • How Ya Feelin’ Now?
      • After
    • Digging Deeper into JUnit Assertions
      • Assertions in JUnit
      • Three Schools for Expecting Exceptions
      • After
    • Organizing Your Tests
      • Keeping Tests Consistent with AAA
      • Testing Behavior Versus Testing Methods
      • Relationship Between Test and Production Code
      • The Value of Focused, Single-Purpose Tests
      • Tests as Documentation
      • More on Before and After (Common Initialization and Cleanup)
      • Green Is Good: Keeping Our Tests Relevant
      • After
  • Mastering Manic Mnemonics!
    • FIRST Properties of Good Tests
      • FIRST It Helps to Remember That Good Tests Are FIRST
      • [F]IRST: [F]ast!
      • F[I]RST: [I]solate Your Tests
      • FI®ST: Good Tests Should Be®epeatable
      • FIR[S]T: [S]elf-Validating
      • FIRS[T]: [T]imely
      • After
    • What to Test: The Right-BICEP excerpt
      • [Right]-BICEP: Are the Results Right?
      • Right-[B]ICEP: Boundary Conditions
      • Remembering Boundary Conditions with CORRECT
      • Right-B[I]CEP: Checking Inverse Relationships
      • Right-BICEP: Cross-Checking Using Other Means
      • Right-BIC[E]P: Forcing Error Conditions
      • Right-BICE[P]: Performance Characteristics
      • After
    • Boundary Conditions–The CORRECT Way
      • ©ORRECT: ©onformance
      • C[O]RRECT: [O]rdering
      • CORRECT:®ange
      • COR®ECT:®eference
      • CORR[E]CT: [E]xistence
      • CORRE©T: ©ardinality
      • CORREC[T]: [T]ime
      • After
  • The Bigger Design Picture
    • Refactoring to Cleaner Code excerpt
      • A Little Bit o’ Refactor
      • Finding Better Homes for Our Methods
      • Automated and Manual Refactorings
      • Taking Refactoring Too Far?
      • After
    • Bigger Design Issues
      • The Profile Class and the SRP
      • Extracting a New Class
      • Command-Query Separation
      • The Cost of Maintaining Unit Tests
      • Other Design Thoughts
      • After
    • Using Mock Objects
      • A Testing Challenge
      • Replacing Troublesome Behavior with Stubs
      • Changing Our Design to Support Testing
      • Adding Smarts to Our Stub: Verifying Parameters
      • Simplifying Testing Using a Mock Tool
      • One Last Simplification: Introducing an Injection Tool
      • What’s Important to Get Right When Using Mocks
      • After
    • Refactoring Tests
      • Searching for an Understanding
      • Test Smell: Unnecessary Test Code
      • Test Smell: Missing Abstractions
      • Test Smell: Irrelevant Information
      • Test Smell: Bloated Construction
      • Test Smell: Multiple Assertions
      • Test Smell: Irrelevant Details in Test
      • Test Smell: Misleading Organization
      • Test Smell: Implicit Meaning
      • Adding a New Test
      • After
  • The Bigger Unit Testing Picture
    • Test-Driven Development
      • The Primary Benefit of TDD
      • Starting Simple
      • Adding Another Increment
      • Cleaning Up Our Tests
      • Another Small Increment
      • Supporting Multiple Answers: A Small Design Detour
      • Expanding the Interface
      • Last Tests
      • Tests As Documentation
      • The Rhythm of TDD
      • After
    • Testing Some Tough Stuff
      • Testing Multithreaded Code
      • Testing Databases
      • After
    • Testing on a Project
      • Coming Up to Speed
      • Get on the Same Page With Your Team
      • Convergence With Continuous Integration
      • Code Coverage
      • After
    • Setting UP JUnit in IntelliJ IDEA and NetBeans
      • IntelliJ IDEA
      • NetBeans


Jeff Langr is a veteran software developer with well over three decades experience building software and helping others learn how to do so. Jeff develops software for Outpace Systems, Inc., and still provides help to customers through Langr Software Solutions, Inc. Jeff’s prior Pragmatic Bookshelf titles include Agile in a Flash (with Tim Ottinger) and Modern C++ Programming with Test-Driven Development.

Andy Hunt has written and co-written over a half-dozen previous titles, including the classic The Pragmatic Programmer, Practices of an Agile Developer, Pragmatic Thinking & Learning, and Learn to Program with Minecraft Plugins, and regularly speaks at conferences on software development around the world.

Dave Thomas is a programmer who likes to evangelize cool stuff. He cowrote The Pragmatic Programmer, and was one of the creators of the Agile Manifesto. His book Programming Ruby introduced the Ruby language to the world, and Agile Web Development with Rails helped kickstart the Rails revolution.

Upcoming Author Events

  • 2020-05-30: Jeff Langr
    From Mob to Symphony: A Mob Composing Workshop. Designed for non-technical folks who want to understand what mobbing feels like and why it can be great, this session has them collaborating to compose a song using software. (Mob Programming Conference 2020, Boston, MA, US)