JUnit in Action, Third Edition

Book description

JUnit in Action, Third Edition has been completely rewritten for this release. The book is full of examples that demonstrate JUnit's modern features, including its new architecture; nested, tagged, and dynamic tests; and dependency injection. You'll benefit from author Cătălin Tudose's unique "pyramid" testing strategy, which breaks the testing process into layers and sets you on the path to bug-free code creation.

Table of contents

  1. JUnit in Action
  2. Copyright
  3. dedication
  4. contents
  5. front matter
    1. preface
    2. acknowledgments
    3. about this book
      1. Who should read this book
      2. How this book is organized: A roadmap
      3. About the code
      4. liveBook discussion forum
    4. about the author
    5. about the cover illustration
  6. Part 1. JUnit
  7. 1 JUnit jump-start
    1. 1.1 Proving that a program works
    2. 1.2 Starting from scratch
      1. 1.2.1 Understanding unit testing frameworks
      2. 1.2.2 Adding unit tests
    3. 1.3 Setting up JUnit
    4. 1.4 Testing with JUnit
    5. Summary
  8. 2 Exploring core JUnit
    1. 2.1 Core annotations
      1. 2.1.1 The @DisplayName annotation
      2. 2.1.2 The @Disabled annotation
    2. 2.2 Nested tests
    3. 2.3 Tagged tests
    4. 2.4 Assertions
    5. 2.5 Assumptions
    6. 2.6 Dependency injection in JUnit 5
      1. 2.6.1 TestInfoParameterResolver
      2. 2.6.2 TestReporterParameterResolver
      3. 2.6.3 RepetitionInfoParameterResolver
    7. 2.7 Repeated tests
    8. 2.8 Parameterized tests
    9. 2.9 Dynamic tests
    10. 2.10 Using Hamcrest matchers
    11. Summary
  9. 3 JUnit architecture
    1. 3.1 The concept and importance of software architecture
      1. 3.1.1 Story 1: The telephone directories
      2. 3.1.2 Story 2: The sneakers manufacturer
    2. 3.2 The JUnit 4 architecture
      1. 3.2.1 JUnit 4 modularity
      2. 3.2.2 JUnit 4 runners
      3. 3.2.3 JUnit 4 rules
      4. 3.2.4 Shortcomings of the JUnit 4 architecture
    3. 3.3 The JUnit 5 architecture
      1. 3.3.1 JUnit 5 modularity
      2. 3.3.2 JUnit Platform
      3. 3.3.3 JUnit Jupiter
      4. 3.3.4 JUnit Vintage
      5. 3.3.5 The big picture of the JUnit 5 architecture
    4. Summary
  10. 4 Migrating from JUnit 4 to JUnit 5
    1. 4.1 Migration steps between JUnit 4 and JUnit 5
    2. 4.2 Needed dependencies
    3. 4.3 Annotations, classes, and methods
      1. 4.3.1 Equivalent annotations, classes, and methods
      2. 4.3.2 Categories vs. tags
      3. 4.3.3 Migrating Hamcrest matcher functionality
      4. 4.3.4 Rules vs. the extension model
      5. 4.3.5 Custom rules
    4. Summary
  11. 5 Software testing principles
    1. 5.1 The need for unit tests
      1. 5.1.1 Allowing greater test coverage
      2. 5.1.2 Increasing team productivity
      3. 5.1.3 Detecting regressions and limiting debugging
      4. 5.1.4 Refactoring with confidence
      5. 5.1.5 Improving implementation
      6. 5.1.6 Documenting expected behavior
      7. 5.1.7 Enabling code coverage and other metrics
    2. 5.2 Test types
      1. 5.2.1 Unit testing
      2. 5.2.2 Integration software testing
      3. System software testing
      4. Acceptance software testing
    3. 5.3 Black-box vs. white-box testing
      1. 5.3.1 Black-box testing
      2. 5.3.2 White-box testing
      3. 5.3.3 Pros and cons
    4. Summary
  12. Part 2. Different testing strategies
  13. 6 Test quality
    1. 6.1 Measuring test coverage
      1. 6.1.1 Introduction to test coverage
      2. 6.1.2 Tools for measuring code coverage
    2. 6.2 Writing testable code
      1. 6.2.1 Understanding that public APIs are contracts
      2. 6.2.2 Reducing dependencies
      3. 6.2.3 Creating simple constructors
      4. 6.2.4 Following the Law of Demeter (Principle of Least Knowledge)
      5. 6.2.5 Avoiding hidden dependencies and global state
      6. 6.2.6 Favoring generic methods
      7. 6.2.7 Favoring composition over inheritance
      8. 6.2.8 Favoring polymorphism over conditionals
    3. 6.3 Test-driven development
      1. 6.3.1 Adapting the development cycle
      2. 6.3.2 Doing the TDD two-step
    4. 6.4 Behavior-driven development
    5. 6.5 Mutation testing
    6. 6.6 Testing in the development cycle
    7. Summary
  14. 7 Coarse-grained testing with stubs
    1. 7.1 Introducing stubs
    2. 7.2 Stubbing an HTTP connection
      1. 7.2.1 Choosing a stubbing solution
      2. 7.2.2 Using Jetty as an embedded server
    3. 7.3 Stubbing the web server resources
      1. 7.3.1 Setting up the first stub test
      2. 7.3.2 Reviewing the first stub test
    4. 7.4 Stubbing the connection
      1. 7.4.1 Producing a custom URL protocol handler
      2. 7.4.2 Creating a JDK HttpURLConnection stub
      3. 7.4.3 Running the test
    5. Summary
  15. 8 Testing with mock objects
    1. 8.1 Introducing mock objects
    2. 8.2 Unit testing with mock objects
    3. 8.3 Refactoring with mock objects
      1. 8.3.1 Refactoring example
      2. 8.3.2 Refactoring considerations
    4. 8.4 Mocking an HTTP connection
      1. 8.4.1 Defining the mock objects
      2. 8.4.2 Testing a sample method
      3. 8.4.3 Try #1: Easy refactoring technique
      4. 8.4.4 Try #2: Refactoring by using a class factory
    5. 8.5 Using mocks as Trojan horses
    6. 8.6 Introducing mock frameworks
      1. 8.6.1 Using EasyMock
      2. 8.6.2 Using JMock
      3. 8.6.3 Using Mockito
    7. Summary
  16. 9 In-container testing
    1. 9.1 Limitations of standard unit testing
    2. 9.2 The mock-objects solution
    3. 9.3 The step to in-container testing
      1. 9.3.1 Implementation strategies
      2. 9.3.2 In-container testing frameworks
    4. 9.4 Comparing stubs, mock objects, and in-container testing
      1. 9.4.1 Stubs evaluation
      2. 9.4.2 Mock-objects evaluation
      3. 9.4.3 In-container testing evaluation
    5. 9.5 Testing with Arquillian
    6. Summary
  17. Part 3. Working with JUnit 5 and other tools
  18. 10 Running JUnit tests from Maven 3
    1. 10.1 Setting up a Maven project
    2. 10.2 Using the Maven plugins
      1. 10.2.1 Maven compiler plugin
      2. 10.2.2 Maven Surefire plugin
      3. 10.2.3 Generating HTML JUnit reports with Maven
    3. 10.3 Putting it all together
    4. 10.4 Maven challenges
    5. Summary
  19. 11 Running JUnit tests from Gradle 6
    1. 11.1 Introducing Gradle
    2. 11.2 Setting up a Gradle project
    3. 11.3 Using Gradle plugins
    4. 11.4 Creating a Gradle project from scratch and testing it with JUnit 5
    5. 11.5 Comparing Gradle and Maven
    6. Summary
  20. 12 JUnit 5 IDE support
    1. 12.1 Using JUnit 5 with IntelliJ IDEA
    2. 12.2 Using JUnit 5 with Eclipse
    3. 12.3 Using JUnit 5 with NetBeans
    4. 12.4 Comparing JUnit 5 usage in IntelliJ, Eclipse, and NetBeans
    5. Summary
  21. 13 Continuous integration with JUnit 5
    1. 13.1 Continuous integration testing
    2. 13.2 Introducing Jenkins
    3. 13.3 Practicing CI on a team
    4. 13.4 Configuring Jenkins
    5. 13.5 Working on tasks in a CI environment
    6. Summary
  22. Part 4. Working with modern frameworks and JUnit 5
  23. 14 JUnit 5 extension model
    1. 14.1 Introducing the JUnit 5 extension model
    2. 14.2 Creating a JUnit 5 extension
    3. 14.3 Writing JUnit 5 tests using the available extension points
      1. 14.3.1 Persisting passengers to a database
      2. 14.3.2 Checking the uniqueness of passengers
    4. Summary
  24. 15 Presentation-layer testing
    1. 15.1 Choosing a testing framework
    2. 15.2 Introducing HtmlUnit
      1. 15.2.1 A live example
    3. 15.3 Writing HtmlUnit tests
      1. 15.3.1 HTML assertions
      2. 15.3.2 Testing for a specific web browser
      3. 15.3.3 Testing more than one web browser
      4. 15.3.4 Creating standalone tests
      5. 15.3.5 Testing forms
      6. 15.3.6 Testing JavaScript
    4. 15.4 Introducing Selenium
    5. 15.5 Writing Selenium tests
      1. 15.5.1 Testing for a specific web browser
      2. 15.5.2 Testing navigation using a web browser
      3. 15.5.3 Testing more than one web browser
      4. 15.5.4 Testing Google search and navigation using different web browsers
      5. 15.5.5 Testing website authentication
    6. 15.6 HtmlUnit vs. Selenium
    7. Summary
  25. 16 Testing Spring applications
    1. 16.1 Introducing the Spring Framework
    2. 16.2 Introducing dependency injection
    3. 16.3 Using and testing a Spring application
      1. 16.3.1 Creating the Spring context programmatically
      2. 16.3.2 Using the Spring TestContext framework
    4. 16.4 Using SpringExtension for JUnit Jupiter
    5. 16.5 Adding a new feature and testing it with JUnit 5
    6. Summary
  26. 17 Testing Spring Boot applications
    1. 17.1 Introducing Spring Boot
    2. 17.2 Creating a project with Spring Initializr
    3. 17.3 Moving the Spring application to Spring Boot
    4. 17.4 Implementing a test-specific configuration for Spring Boot
    5. 17.5 Adding and testing a new feature in the Spring Boot application
    6. Summary
  27. 18 Testing a REST API
    1. 18.1 Introducing REST applications
    2. 18.2 Creating a RESTful API to manage one entity
    3. 18.3 Creating a RESTful API to manage two related entities
    4. 18.4 Testing the RESTful API
    5. Summary
  28. 19 Testing database applications
    1. 19.1 The database unit testing impedance mismatch
      1. 19.1.1 Unit tests must exercise code in isolation
      2. 19.1.2 Unit tests must be easy to write and run
      3. 19.1.3 Unit tests must be fast to run
    2. 19.2 Testing a JDBC application
    3. 19.3 Testing a Spring JDBC application
    4. 19.4 Testing a Hibernate application
    5. 19.5 Testing a Spring Hibernate application
    6. 19.6 Comparing the approaches for testing database applications
    7. Summary
  29. Part 5. Developing applicationswith JUnit 5
  30. 20 Test-driven development with JUnit 5
    1. 20.1 TDD main concepts
    2. 20.2 The flight-management application
    3. 20.3 Preparing the flight-management application for TDD
    4. 20.4 Refactoring the flight-management application
    5. 20.5 Introducing new features using TDD
      1. 20.5.1 Adding a premium flight
      2. 20.5.2 Adding a passenger only once
    6. Summary
  31. 21 Behavior-driven development with JUnit 5
    1. 21.1 Introducing behavior-driven development
      1. 21.1.1 Introducing a new feature
      2. 21.1.2 From requirements analysis to acceptance criteria
      3. 21.1.3 BDD benefits and challenges
    2. 21.2 Working BDD style with Cucumber and JUnit 5
      1. 21.2.1 Introducing Cucumber
      2. 21.2.2 Moving a TDD feature to Cucumber
      3. 21.2.3 Adding a new feature with the help of Cucumber
    3. 21.3 Working BDD style with JBehave and JUnit 5
      1. 21.3.1 Introducing JBehave
      2. 21.3.2 Moving a TDD feature to JBehave
      3. 21.3.3 Adding a new feature with the help of JBehave
    4. 21.4 Comparing Cucumber and JBehave
    5. Summary
  32. 22 Implementing a test pyramid strategy with JUnit 5
    1. 22.1 Software testing levels
    2. 22.2 Unit testing: Basic components working in isolation
    3. 22.3 Integration testing: Units combined into a group
    4. 22.4 System testing: Looking at the complete software
      1. 22.4.1 Testing with a mock external dependency
      2. 22.4.2 Testing with a partially implemented external dependency
      3. 22.4.3 Testing with the fully implemented external dependency
    5. 22.5 Acceptance testing: Compliance with business requirements
    6. Summary
  33. appendix A. Maven
    1. A.1 Convention over configuration
    2. A.2 Strong dependency management
    3. A.3 Maven build life cycles
    4. A.4 Plugin-based architecture
    5. A.5 The Maven project object model (POM)
    6. A.6 Installing Maven
  34. appendix B. Gradle
    1. B.1 Installing Gradle
    2. B.2 Creating Gradle tasks
  35. appendix C. IDEs
    1. C.1 Installing IntelliJ IDEA
    2. C.2 Installing Eclipse
    3. C.3 Installing NetBeans
  36. appendix D. Jenkins
  37. index

Product information

  • Title: JUnit in Action, Third Edition
  • Author(s): Catalin Tudose
  • Release date: January 2021
  • Publisher(s): Manning Publications
  • ISBN: 9781617297045