1

I want to mock an Android drawable for some unit tests. I'm using the @Mockk annotations. This is the code:

@MockK
lateinit var mockIcon: Drawable

@Before
fun setUp() {
    MockKAnnotations.init(this)
}

When I run this code, I get this error lateinit property mockIcon has not been initialized

But when I use the mockk() function instead, the code runs successfully:

val mockIcon: Drawable = mockk()

I don't understand what causes this issue. Is there a difference between the @Mockk annotation and mockk() function?

Thanks in advance

Toni Joe
  • 7,715
  • 12
  • 50
  • 69
  • Generally don't want to mock android classes. Junit is java and doesnt know about the android stack. – JakeB Jul 21 '23 at 13:11
  • Yes, I'm aware of that. I just need to use the mock as a placeholder for some dependency in a model class. Something like: `val tModel = MyModel(icon = mockIcon,...)` – Toni Joe Jul 21 '23 at 14:10
  • 1
    Can you show more codes? I made a simple test and it passed. My code : https://pastebin.com/xLzKAYgQ – ObscureCookie Jul 22 '23 at 09:17

0 Answers0