Mockito Mock Example

In this lesson with Mockito, we will learn what is at the core of Mockito, which surprisingly is, mocks!

Mock is an object that has predefined answers to method executions made during the test and has recorded expectations of these executions.
Steps for creating Mockito TestNG example.
Step 1: Create a simple java maven project.

Adding to classpath, using Maven

Step 2: The fastest way to add Mockito to your project is using Maven dependency.

This dependency is simple enough and does not bring any additional or redundant libraries. See here for latest versions of the library.

As we will also be using some JUnit functionality, we will also need it’s dependency. Let’s add it next,

Your pom.xml will look as below:

Creating a Mock

A mock needs to be created before it is actually used, just like anything else. We can use the mock() method in four separate ways to create a mock.

  • mock(Class<T> classBeingMocked): This method creates a mock of a given class with a default answer set to returning default values. This is the most used method in tests.
  • mock(Class<T> classBeingMocked, String name): This method creates a mock of a given class with a default answer set to returning default values. It also sets a name to the mock. This name is present in all verification messages. That’s very useful in debugging, since it allows you to distinguish the mocks.
  • mock(Class<T> classBeingMocked, Answer defaultAnswer): This method creates a mock of a given class. In other words, all of the nonstubbed mock’s method will act as defined in the passed answer.
  • mock(Class<T> classBeingMocked, MockSettings mockSettings): This method creates a mock of a given class with customizable mock settings. You should hardly ever need to use that feature.

Using mock()

Step 3: Let’s say you have a Customer class and you want to test calculate bill method of customer class. Bill calculation will depend on list of items customer purchased. We will use Mockito’s mock method to actually mock the items and add to the list.

In this way, we will be able to test calculate bill method without actually adding real items to listOfItems.

Let’s create a Customer class.

Create an interface named Item as below.

Now let’s create a test class which will actually test customer’s calculateBill method.

We have mocked Item i1,i2 and i3 here and used Mockito’s when,thenReturn method to mock behaviour in case getName and getPrice method getCalled.
Step 4: When you run the program, you will get below output:

Mockito Mock result

Another example

Code that instantiates objects using the new keyword directly makes testing difficult. By using the Factory pattern you move the creation of the object outside of the code to be tested. This gives you a place to inject a mock object and/or mock factory.

We start by creating a testable Foo class and FooFactory implementation.

Now, our test class:

Now you can pass in a mock factory in the test that will create a mock Foo or a Foo of your choosing. Let’s use Mockito to create the mock factory.

Note: Now, the factory that instantiates Foo must be able to access the constructor, so, it should not be made private.

What are answers? When to use them?

In our examples above, we used thenReturn(…) because we simply wanted to return a mocked object i.e., Foo. But what if we want to do some processing based on the parameters passed to the method, create(100, 101) in our case.

thenAnswer() vs thenReturn()

Answer specifies an action that is executed and a return value that is returned when you interact with the mock.

Simplest example to use a parameter which was passed to when(…) is to return it. This can only be done using thenAnswer(…).

In above example, we get the parameters and just use it in creating a Foo instance using factory method after swapping them.

Mocking with Annotation with different default answers

Usually, you won’t ever need to create a custom answer for Mockito, there are plenty of them already bundled in Mockito and there is no need to reinvent the wheel. Why would you want to create a custom answer anyway? Let’s take a look at a couple of possible answers to that point:

  • It is probable that for debugging the app, you would like to log the arguments that were passed to the stubbed method.
  • You want to perform some business logic on the passed argument rather than just return some fixed value.
  • You want to stub asynchronous methods that have callbacks.

If you thought it over and still want to create a custom answer, please check if there isn’t one already existing in Mockito.

In the provided Mockito API, you can find the following answers in the AdditionalAnswers class (check the Javadoc of that class for examples):

  • returnsFirstArg: This answer will return the first argument of the invocation
  • returnsSecondArg: This answer returns the second argument of the invocation
  • returnsLastArg: This answer returns the last argument of the invocation
  • returnsArgAt: This answer returns the argument of the invocation provided at the given index
  • delegatesTo: This answer delegates all methods to the delegate (you will, in fact, call the delegate’s method if the method hasn’t already been stubbed)

returnsElementsOf: This answer returns the elements of the provided collection.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *