JUnit in Action, Third Edition
Cătălin Tudose, Petar Tahchiev, Felipe Leme, Vincent Massol, and Gary Gregory
  • MEAP began November 2019
  • Publication in Early 2021 (estimated)
  • ISBN 9781617297045
  • 525 pages (estimated)
  • printed in black & white
free previous edition eBook included
An eBook copy of the previous edition of this book is included at no additional cost. It will be automatically added to your Manning Bookshelf within 24 hours of purchase.

This is the go-to reference for JUnit, for both beginners and experts alike.

Kent R. Spillner
JUnit is the gold standard for unit testing Java applications. Filled with powerful new features designed to automate software testing, JUnit 5 boosts your productivity and helps avoid debugging nightmares. Whether you’re just starting with JUnit or you want to ramp up on the new features, JUnit in Action, Third Edition has you covered. Extensively revised with new code and new chapters, JUnit in Action, Third Edition is an up-to-date guide to smooth software testing. Dozens of hands-on examples illustrate JUnit 5’s innovations for dependency injection, nested testing, parameterized tests, and more. Throughout, you’ll learn how to use JUnit 5 to automate your testing, for a process that consumes less resources, and gives you more time for developing.

About the Technology

If you’re a Java developer, you have to learn JUnit, the de facto standard for testing Java applications for over a decade. JUnit 5 is a complete overhaul of this leading Java unit testing framework, filled with new tools that make it easier than ever to write effective tests. JUnit 5 brings more granularity and avoids the need to import an entire library, allows multiple runners to work simultaneously and, best of all, allows developers to make full use of the new language features of Java 8 and beyond.

About the book

JUnit in Action, Third Edition is a fully revised guide to unit testing Java applications with the latest version of JUnit. It’s full of hands-on techniques for solving real-world testing problems, such as using mocks for testing isolation, automating your testing, and test-driven development. You’ll also get to grips with more than just testing tools, thanks author Cătălin Tudose’s testing strategy pyramid, which lays out how to break your testing down into different layers — from unit testing right through to system and acceptance testing. Practical examples demonstrate each new JUnit 5 feature, such as new architecture, as well as nested tests, tagged tests, dynamic tests, and dependency injection. With full coverage of migrating your JUnit 4 applications to JUnit 5, and integrating JUnit with other build tools and continuous integration tools, JUnit in Action sets you on the path to bug-free code.
Table of Contents detailed table of contents

Part 1: JUnit 5 essentials

1 JUnit 5 jump-start

1.1 Proving that a program works

1.2 Starting from scratch

1.2.1 Understanding unit testing frameworks

1.2.2 Adding unit tests

1.3 Setting up JUnit

1.4 Testing with JUnit

1.5 Summary

2 Exploring Core JUnit 5

2.1 Core annotations

2.1.1 The @DisplayName annotation

2.1.2 The @Disabled annotation

2.2 Nested tests

2.3 Tagged tests

2.4 Assertions

2.5 Assumptions

2.6 Dependency injection in JUnit 5

2.6.1 TestInfoParameterResolver

2.6.2 TestReporterParameterResolver

2.6.3 RepetitionInfoParameterResolver

2.7 Repeated tests

2.8 Parameterized tests

2.9 Dynamic tests

2.10 Using Hamcrest matchers

2.11 Summary

3 JUnit architecture

3.1 The concept and importance of software architecture

3.1.1 Story 1 – the telephone directories books

3.1.2 Story 2 – the sneakers manufacturing company

3.2 JUnit 4 architecture

3.2.1 JUnit 4 modularity

3.2.2 JUnit 4 runners

3.2.3 JUnit 4 rules

3.2.4 Shortcomings of the JUnit 4 architecture

3.3 JUnit 5 architecture

3.3.1 JUnit 5 modularity

3.3.2 JUnit 5 Platform

3.3.3 JUnit 5 Jupiter

3.3.4 JUnit 5 Vintage

3.3.5 The big picture of the JUnit 5 architecture

3.4 Summary

4 Migrating from JUnit 4 to JUnit 5

4.1 The steps between JUnit 4 and JUnit 5

4.2 Needed dependencies

4.3 Annotations, classes and methods

4.3.1 Rules vs. the extension model

4.3.2 Custom rules

4.4 Summary

5 Software Testing Principles

5.1 The need for unit tests

5.1.1 Allowing greater test-coverage

5.1.2 Increasing team productivity

5.1.3 Detecting regressions and limiting debugging

5.1.4 Refactoring with confidence

5.1.5 Improving implementation

5.1.6 Documenting expected behavior

5.1.7 Enabling code coverage and other metrics

5.2 Test types

5.2.1 Unit testing

5.2.2 Integration software testing

5.2.3 System software testing

5.2.4 Acceptance software testing

5.3 Black box vs. white box testing

5.3.1 Black-box testing

5.3.2 White-box testing

5.4 Summary

Part 2: Different testing strategies

6 Test quality

6.1 Measuring test coverage

6.1.1 Introduction to test coverage

6.1.2 Code coverage measuring tools

6.2 Writing testable code

6.2.1 Understand that public APIs are contracts

6.2.2 Reduce dependencies

6.2.3 Create simple constructors

6.2.4 Follow the Law of Demeter (Principle of Least Knowledge)

6.2.5 Avoid hidden dependencies and global state

6.2.6 Favor generic methods

6.2.7 Favor composition over inheritance

6.2.8 Favor polymorphism over conditionals

6.3 Test-Driven Development

6.3.1 Adapting the development cycle

6.3.2 Doing the TDD two-step

6.4 Behavior Driven Development

6.5 Mutation Testing

6.6 Testing in the development cycle

6.7 Summary

7 Coarse-grained testing with stubs

7.1 Introducing stubs

7.2 Stubbing an HTTP connection

7.2.1 Choosing a stubbing solution

7.2.2 Using Jetty as an embedded server

7.3 Stubbing the web server resources

7.3.1 Setting up the first stub test

7.3.2 Reviewing the first stub test

7.4 Stubbing the connection

7.4.1 Producing a custom URL protocol handler

7.4.2 Creating a JDK HttpURLConnection stub

7.4.3 Running the test

7.5 Summary

8 Testing with mock objects

8.1 Introducing mock objects

8.2 Unit testing with mock objects

8.3 Refactoring with Mock objects

8.3.1 Refactoring example

8.3.2 Refactoring considerations

8.4 Mocking an HTTP connection

8.4.1 Defining the mock objects

8.4.2 Testing a sample method

8.4.3 Try #1: easy method refactoring technique

8.4.4 Try #2: refactoring by using a class factory

8.5 Using mocks as Trojan horses

8.6 Introducing Mock frameworks

8.6.1 Using EasyMock.

8.6.2 Using JMock

8.6.3 Using Mockito

8.7 Summary

9 In container testing

9.1 Limitations of standard unit testing

9.2 The mock-objects solution

9.3 The step to in-container testing

9.3.1 Implementation strategies

9.3.2 In-container testing frameworks

9.4 Comparing stubs, mock objects, and in-container testing

9.4.1 Stubs evaluation

9.4.2 Mock objects evaluation

9.4.3 In-container testing evaluation

9.5 Testing with Arquillian

9.6 Summary

Part 3: Working with JUnit 5 and other tools

10 Running JUnit 5 tests from Maven 3

10.1 Setting up a Maven project

10.2 Using the Maven plugins

10.2.1 Maven compiler plugin

10.2.2 Maven surefire plugin

10.2.3 HTML JUnit reports with Maven

10.3 Putting it all together

10.4 Maven challenges

10.5 Summary

11 Running JUnit 5 tests from Gradle 6

11.1 Introducing Gradle

11.2 Setting up a Gradle project

11.3 Using Gradle plugins

11.4 Creating a Gradle project from the scratch and testing it with JUnit 5

11.5 Comparing Gradle and Maven

11.6 Summary

12 JUnit 5 IDE support

12.1 Introducing IDEs

12.2 Using JUnit 5 with IntelliJ IDEA

12.3 Using JUnit 5 with Eclipse

12.4 Using JUnit 5 with NetBeans

12.5 Comparing JUnit 5 usage in IntelliJ, Eclipse and NetBeans

12.6 Summary

13 Continuous integration with JUnit 5

13.1 Continuous integration testing

13.2 Introducing Jenkins

13.3 Jenkins customization

13.4 Practicing continuous integration in a team

13.5 Configuring Jenkins

13.6 Working on tasks in a continuous integration environment

13.7 Summary

Part 4: Working with modern frameworks and JUnit 5

14 JUnit 5 extension model

14.1 Introducing JUnit 5 extension model

14.2 Creating the first JUnit 5 extension

14.3 Writing JUnit 5 tests using the available extension points

14.3.1 Persisting the passengers to a database

14.3.2 Checking the unicity of the passengers

14.4 Summary

15 Presentation Layer Testing

15.1 Choosing a Testing Framework

15.2 Introducing HtmlUnit

15.2.1 A live example

15.3 Writing HtmlUnit tests

15.3.1 HTML Assertions

15.3.2 Testing for a specific web browser

15.3.3 Testing more than one web browser

15.3.4 Creating stand-alone tests

15.3.5 Testing forms

15.3.6 Testing JavaScript

15.4 Introducing Selenium

15.5 Writing Selenium Tests

15.5.1 Testing for a specific web browser

15.5.2 Testing navigation using a web browser

15.5.3 Testing more than one web browser

15.5.4 Testing Google search and navigation using different web browsers

15.5.5 Testing the authentication scenario to a website

15.6 HtmlUnit vs. Selenium

15.7 Summary

16 Testing Spring applications

16.1 Introducing the Spring Framework

16.2 Introducing Dependency Injection

16.3 The first steps to using and testing a Spring application

16.3.1 Creating the Spring context programmatically

16.3.2 Using the Spring TestContext framework

16.4 Using the SpringExtension for JUnit Jupiter

16.5 Adding a new feature and testing it with JUnit 5

16.6 Summary

17 Testing Spring Boot applications

17.1 Introducing Spring Boot

17.2 Creating a project with Spring Initializr

17.3 Moving the Spring application to Spring Boot

17.4 Implementing test specific configuration for Spring Boot

17.5 Introducing and testing a new feature into the Spring Boot application

17.6 Summary

18 Testing a REST API

18.1 Introducing REST applications

18.2 Creating a RESTful API to manage one entity

18.5 Summary

19 Testing database applications

19.1 The Database Unit Testing Impedance Mismatch

19.1.1 Unit tests must exercise code in isolation

19.1.2 Unit tests must be easy to write and run

19.1.3 Unit tests must be fast to run

19.2 Testing a JDBC application

19.3 Testing a Spring JDBC application

19.4 Testing a Hibernate application

19.5 Testing a Spring Hibernate application

19.6 Comparing the approaches of testing database applications

19.7 Summary

Part 5: Developing applications with JUnit 5

20 Test Driven Development with JUnit 5

20.1 Introducing Test Driven Development

20.2 Introducing the flights management application

20.3 Preparing the flights management application for TDD

20.4 Refactoring the flights management application

20.5 Introducing new features by working TDD

20.5.1 Adding a premium flight

20.5.2 Adding a passenger only once

20.6 Summary

21 Behavior Driven Development with JUnit 5

21.1 Introducing Behavior Driven Development

21.1.1 Introducing a new feature

21.1.2 From requirements analysis to acceptance criteria

21.1.3 BDD benefits and challenges

21.2 Working BDD with Cucumber and JUnit 5

21.2.1 Introducing Cucumber

21.2.2 Moving a TDD feature to Cucumber

21.2.3 Adding a new feature with the help of Cucumber

21.3 Working BDD with JBehave and JUnit 5

21.3.1 Introducing JBehave

21.3.2 Moving a TDD feature to JBehave

21.3.3 Adding a new feature with the help of JBehave

21.4 Comparing Cucumber and JBehave

21.5 Summary

22 Implementing a test pyramid strategy with JUnit 5

22.1 Software testing levels

22.2 Unit testing – our basic components work in isolation

22.3 Integration testing – units combined as a group

22.4 System testing – looking at the complete software

22.4.1 Testing with a mock external dependency

22.4.2 Testing with a partially implemented external dependency

22.4.3 Testing with the fully implemented external dependency

22.5 Acceptance testing – compliance with the business requirements

22.6 Summary

Appendixes

Appendix A: A Maven

A.1 Convention over configuration

A.2 Strong dependency management

A.3 Maven build life cycles

A.4 Plugin-based architecture

A.5 The Maven project object model (POM)

A.6 Installing Maven

Appendix B: B Gradle

B.1 Installing Gradle

B.2 Creating Gradle tasks

Appendix C: C IDEs

C.1 Installing IntelliJ IDEA

C.2 Installing Eclipse

C.3 Installing NetBeans

Appendix D: D Jenkins

D.1 Jenkins installation

What's inside

  • Introduction to unit testing
  • Connecting JUnit 5 with build tools such as Maven or Gradle
  • Working with JUnit 5 extensions
  • Develop Test Driven Development and Behavior Driven Development applications
  • Migrating from JUnit 4 to 5

About the reader

For intermediate Java developers.

About the authors

Cătălin Tudose has a PhD in Computer Science, and over 15 years experience as a Senior Java Developer and Technical Team Lead. He works as a Java and Web Technologies Expert at Luxoft Romania, and as a professor at the Faculty of Automation and Computers in Bucharest. Petar Tahchiev, Felipe Leme, and Gary Gregory authored the second edition of JUnit in Action, and Vincent Massol was the author of the first edition.

placing your order...

Don't refresh or navigate away from the page.
Manning Early Access Program (MEAP) Read chapters as they are written, get the finished eBook as soon as it’s ready, and receive the pBook long before it's in bookstores.
print book $35.99 $59.99 pBook + eBook + liveBook
includes previous edition eBook
Additional shipping charges may apply
JUnit in Action, Third Edition (print book) added to cart
continue shopping
go to cart

eBook $33.59 $47.99 3 formats + liveBook
includes previous edition eBook
JUnit in Action, Third Edition (eBook) added to cart
continue shopping
go to cart

Prices displayed in rupees will be charged in USD when you check out.

FREE domestic shipping on three or more pBooks