Mockito is a Java library for mocking.
The following samples are taken from the main documentation which can be found here
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()
called matcher // verification mockedList.add("one") was called mockedList.clear() was called }
If one method has not been called on a mock, 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 allow flexible verification or stubbing.
object s3 extends Specification with Mockito { val mockedList = mock[LinkedList[String]] // stubbing mockedList.get(0) returns "first" mockedList.clear() throws new RuntimeException }
returns returns the expected value
s3.mockedList.get(0)> res2: String = first
throws throws the expected exception
s3.mockedList.clear()> import org.specs.Specificationimport org.specs.mock.Mockitoimport org.mockito.Mockimport java.util.Listimport java.util.LinkedListdefined module s3java.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...
null because get(999) was not stubbed:
s3.mockedList.get(999)> res4: String = null
The number of invocations (atLeast, atMost) can also be checked: Number of invocations
When calls have to happen in a given order of invocations, this can be also checked In order calls
In some rare case, you want the stubbed return values to be a function of the input method parameters: Stubbing with callbacks
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()
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" }
s6.mockedList.get(0)> res8: String = hello
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") }
s7.mockedList.get(0)> res11: String = hello
s7.mockedList.get(0)> res13: String = world
You can create Spies of real objects. When you use a spy then the real methods are called (unless a method was stubbed).
Speficic Return values can be returned on unstubbed methods.
Examples summary