EDIT: I solved this I’m writing a test for a class...
# mockk
p
EDIT: I solved this I’m writing a test for a class handling a 3rd party analytics library. The class under test needs to handler an
Analytics
object, but unfortunately
mockk<Analytics>()
cannot create a mock because of a static android Handler in that class:
Copy code
public class Analytics { // this is 3rd party code I can't change

  static final Handler HANDLER =
      new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
          throw new AssertionError("Unknown handler message received: " + msg.what);
        }
      };
// rest of class here
}
I tried using both
mockk<Analytics>()
and adding
mockkConstructor(Handler::class)
but that didn’t work. Any suggestions?
For those not in Android world, the Handler class is not present in JVM unit tests, so the failure message states
java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked
actually, I didn’t realize until I copy / pasted that error message that it was complaining about Looper and not Handler
oops
I think I can fix this
Ah, yes, as soon as I added this snippet, my test works!
Copy code
mockkStatic(Looper::class)
        every { Looper.getMainLooper() } returns mockk()
thank you to everyone here for helping me rubber duck debug rubber duck
m
🙂
950 Views