0%

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
    4. Who should read this book
    5. How this book is organized: A roadmap
    6. About the code
    7. liveBook discussion forum
    8. about the author
    9. 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
    3. 1.2.1 Understanding unit testing frameworks
    4. 1.2.2 Adding unit tests
    5. 1.3 Setting up JUnit
    6. 1.4 Testing with JUnit
    7. Summary
  8. 2 Exploring core JUnit
    1. 2.1 Core annotations
    2. 2.1.1 The @DisplayName annotation
    3. 2.1.2 The @Disabled annotation
    4. 2.2 Nested tests
    5. 2.3 Tagged tests
    6. 2.4 Assertions
    7. 2.5 Assumptions
    8. 2.6 Dependency injection in JUnit 5
    9. 2.6.1 TestInfoParameterResolver
    10. 2.6.2 TestReporterParameterResolver
    11. 2.6.3 RepetitionInfoParameterResolver
    12. 2.7 Repeated tests
    13. 2.8 Parameterized tests
    14. 2.9 Dynamic tests
    15. 2.10 Using Hamcrest matchers
    16. Summary
  9. 3 JUnit architecture
    1. 3.1 The concept and importance of software architecture
    2. 3.1.1 Story 1: The telephone directories
    3. 3.1.2 Story 2: The sneakers manufacturer
    4. 3.2 The JUnit 4 architecture
    5. 3.2.1 JUnit 4 modularity
    6. 3.2.2 JUnit 4 runners
    7. 3.2.3 JUnit 4 rules
    8. 3.2.4 Shortcomings of the JUnit 4 architecture
    9. 3.3 The JUnit 5 architecture
    10. 3.3.1 JUnit 5 modularity
    11. 3.3.2 JUnit Platform
    12. 3.3.3 JUnit Jupiter
    13. 3.3.4 JUnit Vintage
    14. 3.3.5 The big picture of the JUnit 5 architecture
    15. 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
    4. 4.3.1 Equivalent annotations, classes, and methods
    5. 4.3.2 Categories vs. tags
    6. 4.3.3 Migrating Hamcrest matcher functionality
    7. 4.3.4 Rules vs. the extension model
    8. 4.3.5 Custom rules
    9. Summary
  11. 5 Software testing principles
    1. 5.1 The need for unit tests
    2. 5.1.1 Allowing greater test coverage
    3. 5.1.2 Increasing team productivity
    4. 5.1.3 Detecting regressions and limiting debugging
    5. 5.1.4 Refactoring with confidence
    6. 5.1.5 Improving implementation
    7. 5.1.6 Documenting expected behavior
    8. 5.1.7 Enabling code coverage and other metrics
    9. 5.2 Test types
    10. 5.2.1 Unit testing
    11. 5.2.2 Integration software testing
    12. System software testing
    13. Acceptance software testing
    14. 5.3 Black-box vs. white-box testing
    15. 5.3.1 Black-box testing
    16. 5.3.2 White-box testing
    17. 5.3.3 Pros and cons
    18. Summary
  12. Part 2. Different testing strategies
  13. 6 Test quality
    1. 6.1 Measuring test coverage
    2. 6.1.1 Introduction to test coverage
    3. 6.1.2 Tools for measuring code coverage
    4. 6.2 Writing testable code
    5. 6.2.1 Understanding that public APIs are contracts
    6. 6.2.2 Reducing dependencies
    7. 6.2.3 Creating simple constructors
    8. 6.2.4 Following the Law of Demeter (Principle of Least Knowledge)
    9. 6.2.5 Avoiding hidden dependencies and global state
    10. 6.2.6 Favoring generic methods
    11. 6.2.7 Favoring composition over inheritance
    12. 6.2.8 Favoring polymorphism over conditionals
    13. 6.3 Test-driven development
    14. 6.3.1 Adapting the development cycle
    15. 6.3.2 Doing the TDD two-step
    16. 6.4 Behavior-driven development
    17. 6.5 Mutation testing
    18. 6.6 Testing in the development cycle
    19. Summary
  14. 7 Coarse-grained testing with stubs
    1. 7.1 Introducing stubs
    2. 7.2 Stubbing an HTTP connection
    3. 7.2.1 Choosing a stubbing solution
    4. 7.2.2 Using Jetty as an embedded server
    5. 7.3 Stubbing the web server resources
    6. 7.3.1 Setting up the first stub test
    7. 7.3.2 Reviewing the first stub test
    8. 7.4 Stubbing the connection
    9. 7.4.1 Producing a custom URL protocol handler
    10. 7.4.2 Creating a JDK HttpURLConnection stub
    11. 7.4.3 Running the test
    12. 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
    4. 8.3.1 Refactoring example
    5. 8.3.2 Refactoring considerations
    6. 8.4 Mocking an HTTP connection
    7. 8.4.1 Defining the mock objects
    8. 8.4.2 Testing a sample method
    9. 8.4.3 Try #1: Easy refactoring technique
    10. 8.4.4 Try #2: Refactoring by using a class factory
    11. 8.5 Using mocks as Trojan horses
    12. 8.6 Introducing mock frameworks
    13. 8.6.1 Using EasyMock
    14. 8.6.2 Using JMock
    15. 8.6.3 Using Mockito
    16. 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
    4. 9.3.1 Implementation strategies
    5. 9.3.2 In-container testing frameworks
    6. 9.4 Comparing stubs, mock objects, and in-container testing
    7. 9.4.1 Stubs evaluation
    8. 9.4.2 Mock-objects evaluation
    9. 9.4.3 In-container testing evaluation
    10. 9.5 Testing with Arquillian
    11. 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
    3. 10.2.1 Maven compiler plugin
    4. 10.2.2 Maven Surefire plugin
    5. 10.2.3 Generating HTML JUnit reports with Maven
    6. 10.3 Putting it all together
    7. 10.4 Maven challenges
    8. 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
    4. 14.3.1 Persisting passengers to a database
    5. 14.3.2 Checking the uniqueness of passengers
    6. Summary
  24. 15 Presentation-layer testing
    1. 15.1 Choosing a testing framework
    2. 15.2 Introducing HtmlUnit
    3. 15.2.1 A live example
    4. 15.3 Writing HtmlUnit tests
    5. 15.3.1 HTML assertions
    6. 15.3.2 Testing for a specific web browser
    7. 15.3.3 Testing more than one web browser
    8. 15.3.4 Creating standalone tests
    9. 15.3.5 Testing forms
    10. 15.3.6 Testing JavaScript
    11. 15.4 Introducing Selenium
    12. 15.5 Writing Selenium tests
    13. 15.5.1 Testing for a specific web browser
    14. 15.5.2 Testing navigation using a web browser
    15. 15.5.3 Testing more than one web browser
    16. 15.5.4 Testing Google search and navigation using different web browsers
    17. 15.5.5 Testing website authentication
    18. 15.6 HtmlUnit vs. Selenium
    19. 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
    4. 16.3.1 Creating the Spring context programmatically
    5. 16.3.2 Using the Spring TestContext framework
    6. 16.4 Using SpringExtension for JUnit Jupiter
    7. 16.5 Adding a new feature and testing it with JUnit 5
    8. 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
    2. 19.1.1 Unit tests must exercise code in isolation
    3. 19.1.2 Unit tests must be easy to write and run
    4. 19.1.3 Unit tests must be fast to run
    5. 19.2 Testing a JDBC application
    6. 19.3 Testing a Spring JDBC application
    7. 19.4 Testing a Hibernate application
    8. 19.5 Testing a Spring Hibernate application
    9. 19.6 Comparing the approaches for testing database applications
    10. 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
    6. 20.5.1 Adding a premium flight
    7. 20.5.2 Adding a passenger only once
    8. Summary
  31. 21 Behavior-driven development with JUnit 5
    1. 21.1 Introducing behavior-driven development
    2. 21.1.1 Introducing a new feature
    3. 21.1.2 From requirements analysis to acceptance criteria
    4. 21.1.3 BDD benefits and challenges
    5. 21.2 Working BDD style with Cucumber and JUnit 5
    6. 21.2.1 Introducing Cucumber
    7. 21.2.2 Moving a TDD feature to Cucumber
    8. 21.2.3 Adding a new feature with the help of Cucumber
    9. 21.3 Working BDD style with JBehave and JUnit 5
    10. 21.3.1 Introducing JBehave
    11. 21.3.2 Moving a TDD feature to JBehave
    12. 21.3.3 Adding a new feature with the help of JBehave
    13. 21.4 Comparing Cucumber and JBehave
    14. 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
    5. 22.4.1 Testing with a mock external dependency
    6. 22.4.2 Testing with a partially implemented external dependency
    7. 22.4.3 Testing with the fully implemented external dependency
    8. 22.5 Acceptance testing: Compliance with business requirements
    9. 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
44.222.129.73