Testing with JUnit

Book description

Master high quality software development driven by unit tests

About This Book

  • Design and implement robust system components by means of the de facto unit testing standard in Java
  • Reduce defect rate and maintenance effort, plus simultaneously increase code quality and development pace
  • Follow a step-by-step tutorial imparting the essential techniques based on real-world scenarios and code walkthroughs

Who This Book Is For

No matter what your specific background as a Java developer, whether you're simply interested in building up a safety net to reduce regressions of your desktop application or in improving your server-side reliability based on robust and reusable components, unit testing is the way to go. This book provides you with a comprehensive but concise entrance advancing your knowledge step-wise to a professional level.

What You Will Learn

  • Organize your test infrastructure and resources reasonably
  • Understand and write well structured tests
  • Decompose your requirements into small and independently testable units
  • Increase your testing efficiency with on-the-fly generated stand-in components and deal with the particularities of exceptional flow
  • Employ runners to adjust to specific test demands
  • Use rules to increase testing safety and reduce boilerplate
  • Use third party supplements to improve the expressiveness of your verification statements

In Detail

JUnit has matured to become the most important tool when it comes to automated developer tests in Java. Supported by all IDEs and build systems, it empowers programmers to deliver software features reliably and efficiently. However, writing good unit tests is a skill that needs to be learned; otherwise it's all too easy to end up in gridlocked development due to messed up production and testing code. Acquiring the best practices for unit testing will help you to prevent such problems and lead your projects to success with respect to quality and costs.

This book explains JUnit concepts and best practices applied to the test first approach, a foundation for high quality Java components delivered in time and budget.

From the beginning you'll be guided continuously through a practically relevant example and pick up background knowledge and development techniques step by step. Starting with the basics of tests organization you'll soon comprehend the necessity of well structured tests and delve into the relationship of requirement decomposition and the many-faceted world of test double usage. In conjunction with third-party tools you'll be trained in writing your tests efficiently, adapt your test case environment to particular demands and increase the expressiveness of your verification statements. Finally, you'll experience continuous integration as the perfect complement to support short feedback cycles and quality related reports for your whole team.

The tutorial gives a profound entry point in the essentials of unit testing with JUnit and prepares you for test-related daily work challenges.

Style and approach

This is an intelligible tutorial based on an ongoing and non-trivial development example. Profound introductions of concepts and techniques are provided stepwise as the programming challenges evolve. This allows you to reproduce and practice the individual skills thoroughly.

Table of contents

  1. Testing with JUnit
    1. Table of Contents
    2. Testing with JUnit
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Downloading the color images of this book
        3. Errata
        4. Piracy
        5. Questions
    8. 1. Getting Started
      1. Why you should busy yourself with unit tests
        1. Reducing the defect rate
        2. Improving the code quality
        3. Increasing the development pace
        4. Enhancing the specification density
        5. Boosting confidence and courage
      2. Setting the table
        1. Choosing the ingredients
        2. Organizing your code
      3. Serving the starter
        1. Introducing the example app
      4. Understanding the nature of a unit test
        1. Writing the first test
        2. Evaluating test results
        3. Writing tests first
      5. Summary
    9. 2. Writing Well-structured Tests
      1. The four phases of a unit test
        1. Using a common test structure
        2. Setting up the fixture
        3. What goes up must come down
        4. Verification
      2. Choosing the next functionality to test
        1. Start with the happy path
        2. Conclude with corner cases
        3. After the war
      3. Getting the test names right
        1. Test prefix
        2. Behavior-expressing patterns
        3. Reducing names to the essentials
      4. Summary
    10. 3. Developing Independently Testable Units
      1. Decomposing requirements
        1. Separating concerns
        2. Component dependencies
      2. Understanding isolation
        1. Delegating responsibilities to DOCs
        2. Indirect input and output
        3. Unit isolation with test doubles
      3. Working with test doubles
        1. Placeholder dummies
        2. Fake it till you make it
        3. Providing indirect input with stubs
        4. Recording interactions with spies
        5. Verifying behavior with mocks
      4. Increasing efficiency with mock frameworks
        1. The promised land?
        2. Basic stubbing
        3. Indirect output verification
      5. Using test helpers
        1. Motivation
        2. The test helper class
        3. The test helper object
      6. Summary
    11. 4. Testing Exceptional Flow
      1. Testing patterns
        1. Using the fail statement
        2. Annotated expectations
        3. Verification with the ExpectedException rule
        4. Capturing exceptions with closures
      2. Treating collaborators
        1. Fail fast
        2. The stubbing of exceptional behavior
      3. Summary
    12. 5. Using Runners for Particular Testing Purposes
      1. Understanding the architecture
        1. What are runners good for?
        2. Looking at the big picture
        3. Writing your own extension
      2. Using custom runners
        1. Furnishing a suite with test cases
        2. Structuring suites into categories
        3. Populating suites automatically
        4. How about creating test doubles with annotations?
      3. Writing dataset tests
        1. Using parameterized tests
        2. Reducing glue code with JUnitParams
        3. Increasing the expressiveness of test descriptions with Burst
      4. Summary
    13. 6. Reducing Boilerplate with JUnit Rules
      1. Understanding rules
        1. What are JUnit rules?
        2. Writing your own rule
        3. Configuring the fixture with annotations
      2. Working with advanced concepts
        1. Using ClassRules
        2. The ordering of rule execution
      3. Employing custom solutions
        1. Working with system settings
        2. Ignoring tests conditionally
      4. Summary
    14. 7. Improving Readability with Custom Assertions
      1. Working with the JUnit built-in assert approach
        1. Understanding the basics
        2. Reviewing the file session storage
        3. Verifying the storage behavior
        4. Improving readability with assertion helpers
      2. Creating flexible expressions of intent with Hamcrest
        1. Using matcher expressions
        2. Writing custom matchers
      3. Writing fluently readable assertions with AssertJ
        1. Employing assertion chains
        2. Creating your own asserts
      4. Summary
    15. 8. Running Tests Automatically within a CI Build
      1. Wrapping up the sample application
        1. Refining the architecture
        2. Separating concerns into modules
      2. Setting up an automated CI build
        1. What is continuous integration?
        2. Principles and practices
        3. Creating your own build
      3. Integrating code coverage reports
        1. Enlarging on code coverage
        2. Automating reporting
      4. Summary
    16. A. References
      1. Chapter 1
      2. Chapter 2
      3. Chapter 3
      4. Chapter 4
      5. Chapter 5
      6. Chapter 6
      7. Chapter 7
      8. Chapter 8
    17. Index

Product information

  • Title: Testing with JUnit
  • Author(s): Frank Appel
  • Release date: August 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781782166603