2

Am having a challenge, where my test passes when I run it alone but fails when I run all the tests, it shows me this error message :

java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.os.Looper.getMainLooper(Looper.java) at retrofit2.Platform$Android$MainThreadExecutor.(Platform.java:172) at retrofit2.Platform$Android.defaultCallbackExecutor(Platform.java:145) at retrofit2.Retrofit$Builder.build(Retrofit.java:585) at com.andela.mrm.notifications.SlackService.getApi(SlackService.kt:26) at com.andela.mrm.notifications.SlackServiceTest.getAPI(SlackServiceTest.kt:12) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

Below is the Retrofit instance I want to test :

class SlackService {

    private var retrofit: Retrofit? = null

    /**
     * This method creates a new instance of the API interface.
     *
     * @return The API interface
     */
    val api: SlackApi
        get() {
            val baseURL = "https://hooks.slack.com/"
            val client = ApiService.getOkHttpClient()
            if (retrofit == null) {
                retrofit = Retrofit.Builder()
                        .baseUrl(baseURL)
                        .client(client)
                        .build()
            }
            return retrofit!!.create(SlackApi::class.java)
        }
}

And then, this is a test for the Retrofit Instance :

class SlackServiceTest {

    @Test
    fun getAPI() {
        val slackService = SlackService()
        assertNotNull(slackService.api)
    }
}

I managed to see some similar issues via here but they are related with RxJava , and am not using it, how can I be helped ?

Thanks

Idris Stack
  • 546
  • 1
  • 14
  • 36

3 Answers3

3

Adding this fixed the issue for me.

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}
PhantomThief
  • 157
  • 11
1

I found the solution by Liz here and it worked for me. It was a silly mistake. Glad to find a solution after a long trials. https://stackoverflow.com/a/69109928

Pasting the solution for easy reference

In case none of the solutions worked alike in my case, if you are using a later version of Gradle and you are using androix, make sure you import from androix in your build gradle.

implementation 'androidx.arch.core:core-testing:$version'

instead of

implementation 'android.arch.core:core-testing:$version'

Yoganandu
  • 31
  • 6
-1

Actually, I finally got the answer, it's in the link below: Answer

Idris Stack
  • 546
  • 1
  • 14
  • 36