How to Create a Simple Mockito Unit Test

Mockito is a popular Java framework which comes to great help in Unit testing.

In Unit testing it’s important to get to a state where you can concentrate on testing a specific functionality or behaviour. Mockito can help you with this by mocking or simulating specific methods which you may need. It’s important to be to be able to do so because such a method could be relying on an external dependency (such as a database) and you don’t really want to worry about it – for example, to ensure that a database is up and running, has consistent data, etc…

Setting it up

Maven is used for the example setup. In the pom.xml of your project add the following dependency:

		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<version>2.27.0</version>
		</dependency>

If you have troubles with Maven check how to build a Java project with Maven.

Creating an example class

To demonstrate how Mockito works we’ll need a non-final class, i.e. one that Mockito can extend in a way that we’ll instruct it.

In real life this class and its methods would be doing something complex such as getting results from a database. When the database is not available, an exception will be thrown.

For brevity, we’ll write just the exception part and assume that in our development environment, where the JUnit test will be run, there is no database.

public class MyReturner {
	public String returnSomething() throws Exception {
		// in real life, here will be returned the result from a database query
		throw new Exception("Can't find the database.");
	}
}

Creating the Unit test

To avoid getting the exception when there is no database, we’ll mock the method returnSomething() to return a specific result on which we can always rely and build our tests on top of it.

Furthermore, we’ll write two tests – one which passes because we have mocked the method and one which does not because it gets the exception.

import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;


public class MockitoTest {

	private static final String DB_RESULT = "some complex results";

	@Test
	public void testThatPasses() throws Exception {
		MyReturner mockedReturner = mock(MyReturner.class);
		when(mockedReturner.returnSomething()).thenReturn(DB_RESULT);
		assertEquals(mockedReturner.returnSomething(), DB_RESULT);
	}

	@Test
	public void testThatDoesNotPass() throws Exception {
		MyReturner realReturner = new MyReturner();
		assertEquals(realReturner.returnSomething(), DB_RESULT);
	}

}

In our passing test, we have created a mock of the MyReturner class along with a condition what to be returned when the method returnSomething is called. This is accomplished with when / thenReturn.

On the other hand, the unmocked test (testThatDoesNotPass()) fails because the assertTrue comparison fails.

Of course, the above tests are just for demonstration. In real life the tests are expected to do much more than just to assert the result from a method. On the contrary, this is just the fundament of the test.

This is how easy it is to write a simple Unit test with Mockito by mocking the behaviour of an object and eliminating external, unpredictable dependencies which are not directly related to the testing.

Comments

So empty here ... leave a comment!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Sidebar



%d bloggers like this: