Skip to content

Stubbing

Markus Amshove edited this page Feb 21, 2016 · 3 revisions

Stubbing

This file contains examples on supported methods for mocking. All methods use Mockito.

Further examples can be seen in the tests

Setup a mock

val mock = mock(Database::class)

Tell the stub to return an object

val bob = Person("Bob", "Guy")
When calling mock.getPerson() itReturns bob

Tell the stub to throw an exception

When calling mock.getPerson() itThrows RuntimeException("An exception")

Tell the stub to execute an action when called

var counter = 0
When calling mock.getPerson() itAnswers {a -> counter++; alice}
mock.getPerson()
assertEquals(1, counter)