Book Image

Mastering Unit Testing Using Mockito and JUnit

By : Sujoy Acharya
Book Image

Mastering Unit Testing Using Mockito and JUnit

By: Sujoy Acharya

Overview of this book

Table of Contents (17 chapters)
Mastering Unit Testing Using Mockito and JUnit
About the Author
About the Reviewers

Understanding code coverage

Code coverage is a measurement of percentage of instructions of code being executed while the automated tests are running.

A piece of code with high code coverage implies that the code has been thoroughly unit tested and has a lower chance of containing bugs than code with a low code coverage. You should concentrate on writing meaningful (business logic) unit tests and not on having 100 percent coverage because it's easy to cheat and have 100 percent coverage with completely useless tests.

Numerous metrics can be used to measure the code coverage. The following are the ones that are widely used:

  • Statement or line coverage: This measures the statements or lines being covered

  • Branch coverage: This measures the percentage of each branch of each control structure, such as the if else and switch case statements

  • Function or method coverage: This measures the function execution

The following Java code will elucidate the metrics.

An absSum method takes two integer arguments...