Unit Testing

Mockito is a mocking Framework

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>

It allows us to easily mock objects:

mock(MyService.class) //using mock static method

or

@Mock +  MockitoAnnotations.initMocks(this);

We can specify the desired behavior of the mocks using Mockito’s fluent APIs:

when(....)thenReturn(....)

or the equivalent:

    doReturn(object).when(kdskfsk).methodCall

Its also possible to verify side effects using the verify() method chains, however verify should not be a replacement for asserts (see this article).

Advertisements
Tagged with: , ,
Posted in JUnit, Mockito, Testing

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: