https://kotlinlang.org logo
#mockk
Title
# mockk
m

Mattia Tommasone

11/16/2021, 9:24 AM
I think you can achieve something like that by annotating your mocks with
@MockK
and using
MockkAnnotations.init(this, relaxed = true)
m

Matteo Mirk

11/16/2021, 10:32 AM
The project I’m working on uses JUnit 5, so according to the docs I needed to use
@ExtendWith(MockKExtension::class)
which apparently doesn’t allow setting common properties. Then I had to annotate every field with
@RelaxedMockK
and make it
lateinit var
. Not very fond of this solution… I think I’ll revert back to the no annotations form.
m

Mattia Tommasone

11/16/2021, 10:33 AM
not really sure you need to use
@ExtendWith
with JUnit 5 though, i’ve been using annotations and
MockKAnnotations.init()
in my JUnit 5 projects 🙂
m

Matteo Mirk

11/16/2021, 10:48 AM
Oh didn’t know that, I’ll give a try to the manual initialization and let you know. — fact is that properties still need to be
lateinit var
, I’m afraid it would look too noisy for my taste… let’s see 😄
Final result is not too bad, I think I’ll keep it. It works under JUnit 5, thanks; I just had to put the MockkAnnotations call inside an
init {}
otherwise in order to use a @BeforeAll function I’d have to put that in a companion object or add another annotation to the class.
m

Mattia Tommasone

11/16/2021, 11:22 AM
I usually put it in a @BeforeEach function
m

Matteo Mirk

11/16/2021, 11:31 AM
got it thanks!
5 Views