Book description
An advanced guide to mastering unit testing using Mockito and JUnit
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.
What You Will Learn
- 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
Publisher resources
Table of contents
-
Mastering Unit Testing Using Mockito and JUnit
- Table of Contents
- Mastering Unit Testing Using Mockito and JUnit
- Credits
- About the Author
- About the Reviewers
- www.PacktPub.com
- Preface
-
1. JUnit 4 – a Total Recall
- Defining unit testing
- Working with JUnit 4
- Working with JUnit 4++
- Summary
- 2. Automating JUnit Tests
- 3. Test Doubles
-
4. Progressive Mockito
- Working with Mockito
-
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
- Summary
- 5. Exploring Code Coverage
- 6. Revealing Code Quality
- 7. Unit Testing the Web Tier
- 8. Playing with Data
- 9. Solving Test Puzzles
-
10. Best Practices
- Writing meaningful tests
- 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
- Handling exceptions
- Working with test smells
- Summary
- Index
Product information
- Title: Mastering Unit Testing Using Mockito and JUnit
- Author(s):
- Release date: July 2014
- Publisher(s): Packt Publishing
- ISBN: 9781783982509
You might also like
video
Clean Code
Expanded Edition (August 2018) Updated with Design Patterns episodes from the Clean Code series from Clean …
book
The Pragmatic Programmer: your journey to mastery, 20th Anniversary Edition, 2nd Edition
"One of the most significant books in my life." —Obie Fernandez, Author, The Rails Way "Twenty …
book
Mockito Cookbook
Over 65 recipes to get you up and running with unit testing using Mockito In Detail …
book
Hands-On Microservices with Spring Boot and Spring Cloud
Apply microservices patterns to build resilient and scalable distributed systems Key Features Understand the challenges of …