androidkotlinjunitandroid-resourcesmockk

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()

Solution

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

    val mContextMock = mockk<Context>(relaxed = true)