I want to implement some UI Tests to assure that the code implemented today works for tomorrow but when trying to see if already UI tests implemented in the past works, it throws this error:

Caused by: io.mockk.MockKException: Failed matching mocking signature for left matchers: [any(), any()]

This happens on an every {} return Unit line which there's a object file called WakeUpTimeManager, that calls a .set(param1, param2) function and inside that function there are some inline functions which I think it could be causing the problem but I don't know. I tried searching on the internet but couldn't find a solution.

Here's the test that throws the error:

 @Before fun setup() { mockkObject(WakeUpTimerManager) every { WakeUpTimerManager.set(any(), any()) } returns Unit } 

Here's the function that is calling on every line

 fun set(context: Context, timer: Timer) { if (timer.atMillis < System.currentTimeMillis()) { return } if (Preset.findByID(context, timer.presetID) == null) { return } //This is an inline function withGson { PreferenceManager.getDefaultSharedPreferences(context).edit { putString(PREF_WAKE_UP_TIMER, it.toJson(timer)) } } //This is an inline function withAlarmManager(context) { it.setAlarmClock( AlarmManager.AlarmClockInfo(timer.atMillis, getPendingIntentForActivity(context)), getPendingIntentForService(context, timer) ) } } 

Question: Why does mockk throw this error? What's going on? Is there any solution for this?

2 Answers

try with mockkStatic(WakeUpTimerManager::class). For me mockkObject was not working either, but mockkStatic did

In my case I used type cast for any(). I wanted to test that a method viewModel.show(Message()) had invoked. But this method is overloaded (has signatures of different types), so I tried to cast parameter any() to Message.

// show is overloaded method fun show(resourceId: Int) {} fun show(text: String) {} fun show(message: Message) {} // But it threw the exception. verify { viewModel.show(any() as Message) } // This won't work because Message() object will be different verify { viewModel.show(Message()) } 

Maybe mocking for message will help, but not in my case.

// val message = mockk<Message>() // every { Message() } returns message // verify { viewModel.show(message) } 

I had to add mockkStatic, because I used an extension method. For instance, fun ViewExtension.show():

mockkStatic(ViewExtension::class.java.name + "Kt") // Like "com.example...ViewExtensionKt" 

Then mock a behaviour:

every { viewModel.show(Message()) } just Runs verify { viewModel.show(any() as Message) } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy