简体   繁体   中英

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM