Reputation: 4232
I would like to mock a property setter in my test, but I can't find anything on how one does it. Here is what I have tried so far:
interface Foo
{
var property: Int
}
@RunWith(MockitoJUnitRunner::class)
class TestClass
{
@Mock private val fooMock: Foo = mock()
private var temp = 0
@Before
fun setup()
{
whenever(fooMock.property).then {
invocation ->
if (invocation.arguments.isEmpty())
{
// this part works fine
return@then 15
}
else
{
// this never gets called
temp = invocation.getArgument(0)
null
}
}
}
}
note: I am using com.nhaarman.mockito_kotlin library
Upvotes: 2
Views: 6013
Reputation: 89578
A slightly ugly, but working solution for mocking a setter, using a bound property reference to get the setter:
whenever(fooMock::property.setter.invoke(any())).then {
println("setter called with ${it.arguments[0]}")
}
Some alternatives:
If you want to verify that a property was set to a given value, you can do the following:
fooMock.property = 25
verify(fooMock).property = 25
If you want to capture the value that the property was set to, you can use an ArgumentCaptor
, like so:
class TestClass {
@Mock private lateinit var fooMock: Foo
@Captor private lateinit var captor: ArgumentCaptor<Int>
@Test
fun test() {
fooMock.property = 25
verify(fooMock).property = capture(captor)
assertEquals(25, captor.value)
}
}
General hint for Mockito: these two lines both create a mock, you don't need to use both the @Mock
annotation and the mock()
method.
@Mock private lateinit var fooMock: Foo
private val fooMock: Foo = mock()
Upvotes: 6