Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 456 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Android Navigation Instrumentation test throws Method addObserver must be called on the main thread

#1
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?

Reply

#2
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()][1]][1]


[1]:


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.
Reply

#3
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`][1] 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][2] where most of the tests run. As per documentation, you can use `@UiThreadTest` or `runOnUiThread` as in the top [answer][3].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#4
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.
```Kotlin
runOnUiThread {
navController.setGraph(R.navigation.my_graph)
}
```
Reply

#5
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
Reply

#6
I had this issue when my com.google.gms:google-services libraries conflicted with appcompat version. The issue got resolved when I changed my appcompat version to the following

implementation 'androidx.appcompat:appcompat:1.3.0'
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through