Mastering Unit Testing Using Mockito and JUnit

Mastering Unit Testing Using Mockito and JUnit
eBook: $26.99
Formats: PDF, PacktLib, ePub and Mobi formats
save 20%!
Print + free eBook + free PacktLib access to the book: $70.98    Print cover: $43.99
save 38%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Table of Contents
Sample Chapters
  • Create meaningful and maintainable automated unit tests using advanced JUnit features and the Mockito framework
  • Build an automated continuous integration environment to get real-time feedback on broken code, code coverage, code quality, and integration issues
  • Covers best practices and presents insights on architecture and designs to create faster and reliable unit testing environments

Book Details

Language : English
Paperback : 314 pages [ 235mm x 191mm ]
Release Date : July 2014
ISBN : 1783982500
ISBN 13 : 9781783982509
Author(s) : Sujoy Acharya
Topics and Technologies : All Books, Application Development, Open Source

Table of Contents

Chapter 1: JUnit 4 – a Total Recall
Chapter 2: Automating JUnit Tests
Chapter 3: Test Doubles
Chapter 4: Progressive Mockito
Chapter 5: Exploring Code Coverage
Chapter 6: Revealing Code Quality
Chapter 7: Unit Testing the Web Tier
Chapter 8: Playing with Data
Chapter 9: Solving Test Puzzles
Chapter 10: Best Practices
  • Chapter 1: JUnit 4 – a Total Recall
    • Defining unit testing
    • Working with JUnit 4
      • Setting up Eclipse
      • Running the first unit test
        • Exploring annotations
        • Verifying test conditions with Assertion
      • Working with exception handling
      • Exploring the @RunWith annotation
    • Working with JUnit 4++
      • Ignoring a test
      • Executing tests in order
      • Learning assumptions
      • Exploring the test suite
      • Asserting with assertThat
        • Comparing matchers – equalTo, is, and not
        • Working with compound value matchers – either, both, anyOf, allOf, and not
        • Working with collection matchers – hasItem and hasItems
        • Exploring string matchers – startsWith, endsWith, and containsString
        • Exploring built-in matchers
        • Building a custom matcher
      • Creating parameterized tests
        • Working with parameterized constructors
        • Working with parameterized methods
        • Giving a name
      • Working with timeouts
      • Exploring JUnit theories
        • Externalizing data using @ParametersSuppliedBy and ParameterSupplier
      • Dealing with JUnit rules
        • Playing with the timeout rule
        • Working with the ExpectedException rule
        • Unfolding the TemporaryFolder rule
        • Exploring the ErrorCollector rule
        • Working with the Verifier rule
        • Learning the TestWatcher rule
        • Working with the TestName rule
        • Handling external resources
      • Exploring JUnit categories
    • Summary
  • Chapter 2: Automating JUnit Tests
    • Continuous Integration
      • Benefits of CI
      • Gradle automation
        • Getting started
        • Gradling
        • Gradle plugins
      • Maven project management
        • Installation
        • The Archetype plugin
        • The Project Object Model (POM) file
        • Project dependency
        • The build life cycle
        • The clean life cycle
        • The site life cycle
      • Another neat tool (Ant)
    • Jenkins
      • The Gradle project
      • The Maven project
      • Building the Ant project
    • Summary
  • Chapter 4: Progressive Mockito
    • Working with Mockito
      • Why should you use Mockito?
      • Qualities of unit testing
    • Drinking Mockito
      • Configuring Mockito
      • Mocking in action
        • Mocking objects
        • Stubbing methods
        • Verifying the method invocation
        • Using argument matcher
        • Throwing exceptions
        • Stubbing consecutive calls
        • Stubbing with an Answer
        • Spying objects
        • Stubbing void methods
        • Capturing arguments with ArgumentCaptor
        • Verifying the invocation order
        • Changing the default settings
        • Resetting mock objects
        • Exploring Mockito annotations
        • Working with inline stubbing
        • Determining mocking details
      • Behavior-driven development with Mockito
        • Writing tests in BDD style
        • The BDD syntax
    • Summary
  • Chapter 5: Exploring Code Coverage
    • Understanding code coverage
      • Learning the inner details of code instrumentation
    • Configuring the Eclipse plugin
      • Uncovering the Clover plugin
      • Working with the EclEmma plugin
      • Examining the eCobertura plugin
    • Measuring coverage using Gradle
    • Working with the Maven Cobertura plugin
    • Running the Cobertura Ant task
    • Summary
  • Chapter 6: Revealing Code Quality
    • Understanding the static code analysis
    • Working with the Checkstyle plugin
    • Exploring the FindBugs plugin
    • Working with the PMD plugin
    • Monitoring code quality with SonarQube
      • Running SonarQube
      • Analyzing code with the SonarQube runner
      • Improving quality with the Sonar Eclipse plugin
      • Monitoring quality with Gradle and SonarQube
      • Monitoring quality with Maven and SonarQube
      • Monitoring quality with Ant and SonarQube
    • Getting familiar with false positives
    • Summary
  • Chapter 8: Playing with Data
    • Separating concerns
    • Unit testing the persistence logic
    • Simplifying persistence with Spring
    • Verifying the system integrity
    • Writing integration tests with Spring
    • Summary
  • Chapter 9: Solving Test Puzzles
    • Working with the legacy code
      • Working with testing impediments
    • Designing for testability
      • Identifying constructor issues
      • Realizing initialization issues
      • Working with private methods
      • Working with final methods
      • Exploring static method issues
      • Working with final classes
      • Learning the new operator
      • Exploring static variables and blocks
    • Working with greenfield code
    • Summary
  • Chapter 10: Best Practices
    • Writing meaningful tests
      • Improving readability
      • Breaking everything that could possibly break
      • Ignoring simple test cases
      • Verifying invalid parameters
      • Relying on direct testing
      • Staying away from debugging
      • Avoiding generic matchers
      • Keeping away from @ignore
      • Eluding debug messages
    • Automating JUnit tests
    • Configuring tests
      • Running in-memory tests
      • Staying away from Thread.sleep
      • Keeping unit tests away from the production code
      • Avoiding static variables
      • Assuming the test execution order
      • Loading data from files
      • Invoking super.setUp() and super.tearDown()
      • Staying away from side effects
      • Working with locales
      • Working with dates
    • Working with assertions
      • Using the correct assertion
      • Maintaining the assertEquals parameter order
      • Striving for one assertion per test
    • Handling exceptions
    • Working with test smells
      • Refactoring duplicates
      • Refactoring the test control logic
      • Removing the test logic from the production code
      • Refactoring over engineered tests
    • Summary

Sujoy Acharya

Sujoy Acharya works as a software architect with Siemens Technology and Services Pvt Ltd. (STS). He grew up in a joint family and pursued his graduation in Computer Science and Engineering. His hobbies are watching movies and sitcoms, playing outdoor sports, and reading books.

He likes to research upcoming technologies. His major contributions are in the fields of Java, J2EE, SOA, Ajax, GWT, and the Spring framework.

He has authored Test-Driven Development with Mockito, Packt Publishing.He designs and develops healthcare software products. He has over 10 years of industrial experience and has architected and implemented large-scale enterprise solutions.

Sorry, we don't have any reviews for this title yet.

Code Downloads

Download the code and support files for this book.

Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.

Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

Frequently bought together

Mastering Unit Testing Using Mockito and JUnit +    Inkscape 0.48 Illustrator's Cookbook =
50% Off
the second eBook
Price for both: €27.85

Buy both these recommended eBooks together and get 50% off the cheapest eBook.

What you will learn from this book

  • Write advanced JUnit test cases using advanced JUnit 4 features
  • Automate JUnit test cases using Gradle, Maven, and Ant scripting
  • Practice continuous integration with Jenkins and Ant, Maven, and Gradle scripting
  • Monitor code quality with the SonarQube dashboard
  • Analyze static code using PMD, Checkstyle, and FindBugs
  • Master code coverage with Clover, Cobertura, EclEmma, and JaCoCo
  • Design for testability and refactor legacy code
  • Practice test-driven development with Mockito

In Detail

It is insanity to keep doing things the same way and expect them to improve. Any program is useful only when it is functional; hence, before applying complex tools, patterns, or APIs to your production code, checking software functionality is must. Automated JUnit tests help you verify your assumptions continuously, detect side effects quickly, and also help you save time.

This book will provide the skills you need to successfully build and maintain meaningful JUnit test cases. You will begin with how to use advanced JUnit 4 features, improve code coverage, automate JUnit tests, monitor code quality, write JUnit tests for the database and web tier refactor legacy code, mock external dependencies using Mockito, and write testable code using test-driven development. By sequentially working through the steps in each chapter, you will quickly master the advanced JUnit features.


A practical and easy-to-follow, yet comprehensive, guide to learning advanced JUnit testing. Each topic is explained and placed in context, and for the more inquisitive, there are more details of the concepts used.

Who this book is for

This book is for you if you are a developer with some experience in Java application development as well as a basic knowledge of JUnit testing. But for those whose skill set is void of any prior experience with JUnit testing, the book also covers basic fundamentals to get you acquainted with the concepts before putting them into practise.

Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software