What should I unit test?

Knowing what to test is difficult, but the important question you should always ask yourself is;

“Am I just writing this test to increase the code coverage?”

If the answer is yes, then chances are that test isn’t adding any value. Let’s look at an example I like to use;

package com.liamdjolly;
public class MyException extends Exception {
public MyException() {
public MyException(String message) {
public MyException(String message, Throwable cause) {
super(message, cause);
public MyException(Throwable cause) {
public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);

Here we have a custom exception, something that we probably throw from one of our methods to give us an opportunity to handle a specific case. If I were to write a test for this class, I wouldn’t be testing any functionality as there is none. So the right thing to do is not to write a test.

In this blog post http://labs.ig.com/code-coverage-100-percent-tragedy the author describes developers caught up in the mindset of achieving 100% coverage. These developers aren’t adding any value to their code base, instead they’re just writing tests to increase the coverage.

So when someone asks you, “what should I unit test?” You should say;


Just don’t do it for the sake of increasing code coverage.



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s