Skip to content
Advertisement

Unable to run Robolectric and Espresso with a FragmentContainerView

I have been trying to run Robolectric along with Espresso but, I haven’t been successful yet. After trying to figure out what was causing this, I realized my FragmentContainerView in my activity is causing this and I am not sure how to fix it.

I decided to create a blank project thinking that perhaps something in my project was causing this, and I encountered the same issue on the blank project.

This is the current setup where I am testing this.

MainActivity.java

JavaScript

activity_main.xml

JavaScript

BlankFragment.java

JavaScript

fragment_blank.xml

JavaScript

main_nav.xml

JavaScript

build.gradle(Project)

JavaScript

build.gradle(Module)

JavaScript

MainActivityTest (androidTest) : This instrumentation test runs without issues in the emulator/device as expected.

JavaScript

MainActivityTest (test) : This test is unable to run, it throws the exception below and it will only work if I comment out the FragmentContainerView in the activity_main.xml layout.

JavaScript

Exception received after trying to run the test with robolectric and espresso:

JavaScript

So, I tried using the suggestion in the exception:

JavaScript

But I encountered the same exception:

JavaScript

Any help is greatly appreciated!

Advertisement

Answer

I figured out the issue. What was causing this to not work correctly was the following dependency for navigation component.

JavaScript

After I removed it, it not only fixed my issue with robolectric and espresso but, also another one with navigation testing. I am not sure what happened in the latest version but, since I updated that specific dependency, it doesn’t seem to be compatible with both robolectric and navigation testing.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement