Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock android context using mockk library

I am new in JUnit testing on Android and I'm testing a function, which is using android context object to get a string resources and making some comparsions. How can I mock android context object to successfully test this function? For testing I'm using Mockk testing library.

  private val context = mockk<Context>()
  private val contextWrapper = ApplicationContextWrapper(context)
  private val objectUnderTest = AppLinkService(contextWrapper)

I was trying to mock context using mockk<Context>(), but I'm getting the following exception

io.mockk.MockKException: no answer found for: Context(#1).getApplicationContext()
like image 632
Taras Stavnychyi Avatar asked Sep 09 '19 10:09

Taras Stavnychyi


1 Answers

Ok, I found the answer. Using relaxed mock solved my problem

val mContextMock = mockk<Context>(relaxed = true)
like image 99
Taras Stavnychyi Avatar answered Nov 06 '22 15:11

Taras Stavnychyi