Using Mockito

At a very high level, we can use Mockito to do two things:

  • Provide wrapper implementations over dependent methods in the application class
  • Verify that these wrapper implementations are called

We specify the wrapper implementation using a static method of Mockito:

Mockito.when(object_name.method_name(params)).thenReturn(return_value); 

Further, we verify whether the wrapper method was called by calling another static method of Mockito:

Mockito.verify(object_name, Mockito.atLeastOnce()).method_name(params);

To use Mockito in our project, we need to add a dependency on it in our pom.xml:

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

Before we start ...

Get Java EE 8 Development with Eclipse now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.