Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

870
Visualizações
Can you verify a property setter using mockk?

We had a few tests in Java and Mockito, which we are progressively converting into Kotlin and Mockk. There's a problem, though. This simple line:

verify(mockedInteractor).setIndex(1);

When we move it to mockk, we get this:

verify { mockedInteractor.index = 1 }

This of course passes the tests, as it's not actually checking that index was set to 1. It's simply setting the mock's variable to 1. This below has the same effect.

verify { mockedInteractor.setIndex(1) }

Is there a way to verify setters?

over 4 years ago · Hanz Gallego
4 Respostas
Responde à pergunta

0

You could try capture:

val fooSlot = slot<String>()
val mockBar = mockk<Bar>()
every { mockBar.foo = capture(fooSlot) } answers { }
assertEquals(fooSlot.captured, "expected")
over 4 years ago · Hanz Gallego Relatório

0

Yes you can:

verify { mockedInteractor setProperty "index" value 1 }

There are more examples in here https://mockk.io/#private-functions-mocking--dynamic-calls

over 4 years ago · Hanz Gallego Relatório

0

Compact solution without hardcoded string:

verify { mockedInteractor setProperty MockedInteractor::index.name value 1 }

where MockedInteractor is mockedInteractor class

over 4 years ago · Hanz Gallego Relatório

0

You can now relax this requirement for unit functions when defining your mock.

val foo = mockk<Foo>(relaxUnitFun = true)

Enabling this setting on your mock means you will not need to use justRun or any variation of that code (as per the Mockk documentation) when verifying unit functions are invoked.

over 4 years ago · Hanz Gallego Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda