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

5
  • Show code with other tests. Commented Sep 5, 2019 at 8:31
  • This error happens when you try to execute asynchronous task in your test. Commented Sep 5, 2019 at 8:32
  • There are lots of tests , I cant post them here @ArslanShoukat Commented Sep 5, 2019 at 9:40
  • Yes , it happens when youy try to execute asynchronous task in your task. Commented Sep 5, 2019 at 9:41
  • Could you post at least one another test that you have so we can try out and give you reasonable answer? Commented Sep 5, 2019 at 11:44

3 Answers 3

8

Adding this fixed the issue for me.

android {
  // ...
  testOptions { 
    unitTests.returnDefaultValues = true
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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'

Comments

-1

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

3 Comments

Thank you so much man. It finally works. I have been searching for 40 minutes.
@IdrisStack The site you mentioned here is not available anymore
This is why we put a full answer here, to avoid losing information when sites go offline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.