Mockito Specification

Mockito is a Java library for mocking.

The following samples are taken from the main documentation which can be found here

Let's verify some behaviour

First of all, we need to import some classes and traits for our examples:

import org.specs.Specification
import org.specs.mock.Mockito
import org.mockito.Mock
import java.util.List
import java.util.LinkedList

A mock is created with the mock method:

object s extends Specification with Mockito {

// mock creation
val mockedList = mock[List[String]]

// using mock object
mockedList.add("one")
mockedList.clear()

// It is possible to check that some methods have been called on the mock with the called matcher:

// verification
mockedList.add("one") was called
mockedList.clear() was called
}

Failures

If one method has not been called on a mock, the was called matcher must throw a FailureException:

object s2 extends Specification with Mockito {
val m = mock[List[String]]
m.clear() was called
}
s2.failures

> res0: List[org.specs.execute.FailureException] = List(org.specs.specification.FailureExceptionWithResult: The method was not called as expected: Wanted but not invoked: list.clear();)

Argument matchers

Argument Matchers allow flexible verification or stubbing.

How about some stubbing?

You can mock concrete classes, not only interfaces

object s3 extends Specification with Mockito {
val mockedList = mock[LinkedList[String]]

// stubbing
mockedList.get(0) returns "first"
mockedList.clear() throws new RuntimeException
}

Calling a stubbed method with returns returns the expected value. For example, the following prints "first":

s3.mockedList.get(0)
> res2: String = first

Calling a stubbed method with throws throws the expected exception. For example, the following throws a RuntimeException:

s3.mockedList.clear()
>
import org.specs.Specification
import org.specs.mock.Mockito
import org.mockito.Mock
import java.util.List
import java.util.LinkedList
defined module s3
java.lang.RuntimeException
at .<init>(<console>:9)
at .<clinit>(<console>)
at RequestResult$.<init>(<console>:3)
at RequestResult$.<clinit>(<console>)
at RequestResult$result(<console>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invok...

Calling a non-stubbed method should return a default value. For example, the following returns null because get(999) was not stubbed:

s3.mockedList.get(999)
> res4: String = null

Verifying the number of invocations

The number of invocations (atLeast, atMost) can also be checked: Number of invocations

Verifying that invocations occur in order

When calls have to happen in a given order of invocations, this can be also checked In order calls

Callbacks

In some rare case, you want the stubbed return values to be a function of the input method parameters: Stubbing with callbacks

Annotations

It is possible to use annotations to declare mocks

object s5 extends Specification with Mockito {
// do we gain anything using Scala, compared to val mockedList = mock[List[String]]?
@Mock val mockedList: List[String] = null
"this needs to be inside an example because otherwise a NPE is thrown" in {
mockedList.clear()
mockedList.clear() was called
}
}

s5.isOk
> res5: Boolean = true
> res7: List[Throwable] = List()

Stubbing consecutive calls (iterator-style stubbing)

Sometimes we need to stub with different return value/exception for the same method call. Typical use case could be mocking iterators. Original version of Mockito did not have this feature to promote simple mocking. For example, instead of iterators one could use Iterable or simply collections. Those offer natural ways of stubbing (e.g. using real collections).
In rare scenarios stubbing consecutive calls could be useful, though:

object s6 extends Specification with Mockito {
val mockedList = mock[List[String]]
mockedList.get(0) returns "hello" thenReturns "world"
}

The first call returns the first value:

s6.mockedList.get(0)
> res8: String = hello

The second call returns the second value:

s6.mockedList.get(0)
> res10: String = world

When several values need to be stubbed this version of returns would also work:

object s7 extends Specification with Mockito {
val mockedList = mock[List[String]]
mockedList.get(0) returns ("hello", "world")
}

The first value is "hello":
s7.mockedList.get(0)
> res11: String = hello

The second value is "world":
s7.mockedList.get(0)
> res13: String = world

Spies

You can create Spies of real objects. When you use a spy then the real methods are called (unless a method was stubbed).

Return values

Speficic Return values can be returned on unstubbed methods.

Examples summary