Unit Testing Static Methods With Mockito

Unit testing helps us to cover different scenarios and it is a way to ensure the applications behave as expected under certain circumstances. Most of the time it is easy to test your classes and methods, but sometimes you need to mock certain services or methods to isolate your target. Mockito is a good library to help you with that. It can easily create mocked or partially mocked objects for you with Mockito#mock or with Mockito#spy.

There are some cases that we also want to mock static methods of a particular utility class, so how can we accomplish that? Well by using the Mockito mockStatic method. Lets take an example by using the following AnimalUtils, Dog objects and Animal Interface:

Unit Testing Void Methods with Mockito and JUnit

Writing functionality is the main focus whenever we are writing a software program, but it is equally important that we make sure our code works the way we intended it to. And how do we do that? By writing unit test cases. They are used to test the smallest functionality of code. Unit test cases are an essential part of software development. In this blog, we are going to cover one of the testing scenarios and unit test cases for void methods.

How to Test Void Methods 

As we already know that our aim is to test void methods in a class. But it is also really important to understand why we test void methods. 

Use Mocks in Testing? Choose the Lesser Evil!

Mocking Test Methodology 

The key idea of mocking is to replace real code (e.g. calls to a database or service) with artificial functionality that has the same return type. There are different approaches to this practice. Here, I explain best practices and why it might be better to avoid mocking to have real code quality.

User Service — Example to Be Tested With Mocks

Let's write a simple application that fetches users from HTTP service. 

Beginners Guide to Mocking in Scala

We all know that unit test cases are one of the most important parts of an application. No? Then, I must tell you that unit testing is one of the earliest tests to be performed on the unit of code, and the earlier the defects are detected, the easier it is to fix. It reduces the difficulties of discovering errors contained in more complex pieces of the application.

So where does mocking come into the picture? Why do we need it? And how do we understand what we should mock while writing unit test cases? Answers to these questions are right below in this blog.

How to Unit Test Classes Which Create New Objects

Learn how to conduct effective unit tests.

First of all, I will start with a disclaimer that I am a strong proponent of using the simple factory programming idiom and by extension of using the Factory Method Pattern, instead of creating objects inside classes. The factory idiom helps you to insulate your code to changes thereby adhering to the Open to Extension Close to modification principle of object-oriented programming.

You may also like: Unit Testing: The Good, Bad, and Ugly

Also, the idea is to write testable code, not to write a hack to test code.

Best Java Unit Testing Frameworks

Learn more about the best Java unit testing frameworks.

Unit testing is an important skill for programmers. With that said, what are the best frameworks you can use to write great unit tests in Java?

You may also like: 8 Benefits of Unit Testing

What We Will Learn

  • What are the best Java unit testing frameworks?
  • What is JUnit? How do you use JUnit for unit testing?
  • What is Mockito?
  • What is mocking?
  • How do you write unit tests with Junit, Mockito, AssertJ, and other frameworks?
  • What is the best Java unit testing framework for writing great asserts?

The Base Unit Testing Framework — JUnit/TestNG

Whenever you write a unit test, you execute the code and then check its output. You need a basic framework in place to run a large number of tests in a similar manner.