Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Navigation Instrumentation test throws Method addObserver must be called on the main thread

I have two Fragments, one being a home fragment in my graph. The User will be navigated to the second fragment upon clicking a button. It works as expected by navigating the user to the second fragment and displaying the text. So the graph is good.

Now I wanted to write an instrumentation test.

@RunWith(AndroidJUnit4::class)
class TransitionTest {

    @Test
    fun testNavigationToSecondFragment() {
        
        val navController = TestNavHostController(
            ApplicationProvider.getApplicationContext())

        navController.setGraph(R.navigation.my_graph) <--throws exception

        // the rest of my test continues here
    }
}

However, the line shown above to set the graph throws following exception:

IllegalStateException: Method addObserver must be called on the main thread.

My environment:

fragment_version = 1.2.5 nav_version = 2.3.1 espresso = 3.3.0

Does anyone have any idea what is going on and how to solve it?

like image 642
The_Martian Avatar asked Nov 04 '20 18:11

The_Martian


People also ask

How do I add navigation testing to my project?

You can add the Navigation Testing artifact to your project by adding the following dependency in your app module's build.gradle file: dependencies { def nav_version = "2.3.5" androidTestImplementation "androidx.navigation:navigation-testing:$nav_version" } Let’s say you are building a trivia game.

How do I test that the app properly navigates the user?

To test that the app properly navigates the user to the in_game screen when the user clicks Play, your test needs to verify that this fragment correctly moves the NavController to the R.id.in_game screen.

When should I use Mockito to verify the navcontroller state?

Note: when using Navigation 2.2.1 or earlier, it is recommended to use a mock NavController with Mockito and verify that the correct actions are taken rather than verify the NavController’s state.

How do I test a fragmentscenario with testnavhostcontroller?

Using a combination of FragmentScenario, Espresso , and TestNavHostController, you can recreate the conditions necessary to test this scenario, as shown in the following example: The above example creates an instance of TestNavHostController and assigns it to the fragment.


4 Answers

I wrapped the setGraph function in runOnUiThread as such, and the test passes. I will update the answer once I find out the real cause and better solution.

runOnUiThread {
    navController.setGraph(R.navigation.my_graph)
}
like image 129
The_Martian Avatar answered Oct 20 '22 23:10

The_Martian


I faced the same, so I will try to explain the root cause of this. Starting from androidx.lifecycle:lifecycle-*:2.3.0-alpha06. There is a behavior change in LifecycleRegistry . LifecycleRegistry now verifies that its methods are called on the main thread. Now remains another question unsolved whether the instrumentation tests are running in the MainThread? Apparently no otherwise it would have passed. It is using Instrumentation Thread where most of the tests run. As per documentation, you can use @UiThreadTest or runOnUiThread as in the top answer.

like image 23
Metwalli Avatar answered Oct 20 '22 21:10

Metwalli


I started having this problem as well, after upgrading the package:

implementation 'androidx.activity:activity:1.2.0-alpha06'

to

 implementation 'androidx.activity:activity:1.2.0-beta01'

In my code I am opening a Fragment in a background thread, and this seems to break with new package. The fragment adds an observer during lifecycle:

Fragment.java initlifecycle()

I think your workaround of wrapping the call to run on UI thread is the only plausible solution, since the observer may be added internally in your case as well.

like image 32
Maurizio Macagno Avatar answered Oct 20 '22 22:10

Maurizio Macagno


There is one more solution to solve the issue by using this

InstrumentationRegistry.getInstrumentation().runOnMainSync {
            navController.setGraph(R.navigation.my_graph)
        }

Since @UiThreadTest only work in @Test, @Before and @After so if you have some common test function they might not work as expected

like image 43
Long Ranger Avatar answered Oct 20 '22 23:10

Long Ranger