Mastering Unit Testing Using Mockito and JUnit
eBook - ePub

Mastering Unit Testing Using Mockito and JUnit

Sujoy Acharya

Share book
  1. 314 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Unit Testing Using Mockito and JUnit

Sujoy Acharya

Book details
Book preview
Table of contents
Citations

About This Book

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.

Approach

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.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Mastering Unit Testing Using Mockito and JUnit an online PDF/ePUB?
Yes, you can access Mastering Unit Testing Using Mockito and JUnit by Sujoy Acharya in PDF and/or ePUB format, as well as other popular books in Informatik & Softwareentwicklung. We have over one million books available in our catalogue for you to explore.

Information

Year
2014
ISBN
9781783982509
Edition
1

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
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
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
2. Automating JUnit Tests
Continuous Integration
Benefits of CI
Gradle automation
Getting started
Gradling
Ordering subtasks using doFirst and doLast
Default tasks
The task dependency
Daemon
Gradle plugins
The Eclipse plugin
The Java plugin
Maven project management
Installation
The Archetype plugin
The Project Object Model (POM) file
Project dependency
The build life cycle
Compiling the project
Testing the project
Packaging the project
The clean life cycle
The site life cycle
Another neat tool (Ant)
Jenkins
The Gradle project
The Maven project
Building the Ant project
Summary
3. Test Doubles
Dummy
Stub
Fake
Mock
Spy
Summary
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
Why do we use verify?
Verifying in depth
Verifying zero and no more interactions
Using argument matcher
Why do we need wildcard matchers?
The ArgumentMatcher class
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
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
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
7. Unit Testing the Web Tier
Unit testing servlets
Building and unit testing a J2EE web application
Playing with Spring MVC
Summary
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
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 attribute
Exploring static variables and blocks
Working with greenfield code
Summary
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
Index

Mastering Unit Test...

Table of contents