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

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainActivityConstraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainerView"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/main_nav" />

</androidx.constraintlayout.widget.ConstraintLayout>

BlankFragment.java

public class BlankFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }
}

fragment_blank.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".BlankFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

main_nav.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_nav"
    app:startDestination="@id/blankFragment">

    <fragment
        android:id="@+id/blankFragment"
        android:name="com.example.robolectrictest.BlankFragment"
        android:label="fragment_blank"
        tools:layout="@layout/fragment_blank" />
</navigation>

build.gradle(Project)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(Module)

plugins {
    id 'com.android.application'
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.example.robolectrictest"
        minSdkVersion 27
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    testOptions {
        animationsDisabled true

        unitTests {
            includeAndroidResources true
            returnDefaultValues = true
        }
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'androidx.navigation:navigation-fragment:2.3.5'
    implementation 'androidx.navigation:navigation-ui:2.3.5'

    testImplementation 'junit:junit:4.13.2'
    testImplementation "org.robolectric:robolectric:4.5.1"
    testImplementation 'androidx.test.espresso:espresso-core:3.3.0'
    testImplementation 'androidx.test.ext:junit:1.1.2'
    testImplementation 'androidx.arch.core:core-testing:2.1.0'
    testImplementation "androidx.test:runner:1.3.0"

    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

    def nav_version = "2.3.5"

    // Java language implementation
    implementation "androidx.navigation:navigation-fragment:$nav_version"
    implementation "androidx.navigation:navigation-ui:$nav_version"

    // Feature module Support
    implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"

    // Testing Navigation
    androidTestImplementation "androidx.navigation:navigation-testing:$nav_version"

    // Jetpack Compose Integration
    implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"
}

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

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> mainActivityRule =
            new ActivityScenarioRule<MainActivity>(MainActivity.class);

    @Test
    public void testMainActivityIsVisible() {
        onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
    }
}

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.

@RunWith(AndroidJUnit4.class)
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> mainActivityRule =
            new ActivityScenarioRule<MainActivity>(MainActivity.class);

    @Test
    public void testMainActivityIsVisible() {
        onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
    }
}

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

java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.

    at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:581)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ExceptionInInitializerError
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:219)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
    at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
    at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
    at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
    at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
    at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
    at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
    at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:7995)
    at android.app.Activity.performCreate(Activity.java:7979)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
    at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:106)
    at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
    at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
    at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
    at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
    at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
    at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
    at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$0$ActivityScenarioRule(ActivityScenarioRule.java:70)
    at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda$0.get(Unknown Source)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:50)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
    ... 6 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
    at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
    at androidx.navigation.NavController.<clinit>(NavController.kt)
    at androidx.navigation.fragment.NavHostFragment.$$robo$$androidx_navigation_fragment_NavHostFragment$onCreate(NavHostFragment.java:219)
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
    at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
    at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
    at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
    at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
    at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
    at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$tryCreateView(LayoutInflater.java:1067)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:995)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:1121)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:479)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at androidx.appcompat.app.AppCompatDelegateImpl.$$robo$$androidx_appcompat_app_AppCompatDelegateImpl$setContentView(AppCompatDelegateImpl.java:699)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java)
    at androidx.appcompat.app.AppCompatActivity.$$robo$$androidx_appcompat_app_AppCompatActivity$setContentView(AppCompatActivity.java:195)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java)
    at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7995)
    at android.app.Activity.performCreate(Activity.java)
    at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7979)
    at android.app.Activity.performCreate(Activity.java)
    at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1309)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
    ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
    at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
    ... 64 more
Caused by: java.lang.IllegalAccessError: failed to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.navigation.NavController$Companion (kotlin.jvm.internal.DefaultConstructorMarker is in unnamed module of loader 'app'; androidx.navigation.NavController$Companion is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @71def8f8)
    at androidx.navigation.NavController$Companion.<init>(NavController.kt)
    at androidx.navigation.NavController.__staticInitializer__(NavController.kt)
    ... 70 more


Process finished with exit code -1

So, I tried using the suggestion in the exception:

@RunWith(AndroidJUnit4.class)
@LooperMode(LooperMode.Mode.PAUSED)
public class MainActivityTest {

    @Rule
    public ActivityScenarioRule<MainActivity> mainActivityRule =
            new ActivityScenarioRule<MainActivity>(MainActivity.class);

    @Test
    public void testMainActivityIsVisible() {
        Shadows.shadowOf(Looper.getMainLooper()).idle();
        onView(withId(R.id.mainActivityConstraintLayout)).check(matches(isDisplayed()));
    }
}

But I encountered the same exception:

java.lang.Exception: Main looper has queued unexecuted runnables. This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.

    at org.robolectric.android.internal.AndroidTestEnvironment.checkStateAfterTestFailure(AndroidTestEnvironment.java:502)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:581)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:278)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.ExceptionInInitializerError
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java:219)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
    at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
    at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
    at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
    at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
    at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
    at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java:1067)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:995)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:479)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:699)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
    at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.performCreate(Activity.java:7995)
    at android.app.Activity.performCreate(Activity.java:7979)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:711)
    at org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:106)
    at org.robolectric.shadows.ShadowPausedLooper.runPaused(ShadowPausedLooper.java:200)
    at org.robolectric.android.controller.ActivityController.create(ActivityController.java:106)
    at org.robolectric.android.controller.ActivityController.create(ActivityController.java:111)
    at org.robolectric.android.fakes.RoboMonitoringInstrumentation.startActivitySyncInternal(RoboMonitoringInstrumentation.java:66)
    at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
    at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
    at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:195)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.lambda$new$0$ActivityScenarioRule(ActivityScenarioRule.java:70)
    at androidx.test.ext.junit.rules.ActivityScenarioRule$$Lambda$0.get(Unknown Source)
    at androidx.test.ext.junit.rules.ActivityScenarioRule.before(ActivityScenarioRule.java:103)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:50)
    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
    at org.robolectric.RobolectricTestRunner$HelperTestRunner$1.evaluate(RobolectricTestRunner.java:575)
    ... 6 more
Caused by: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
    at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:166)
    at org.robolectric.internal.bytecode.RobolectricInternals.classInitializing(RobolectricInternals.java:21)
    at androidx.navigation.NavController.<clinit>(NavController.kt)
    at androidx.navigation.fragment.NavHostFragment.$$robo$$androidx_navigation_fragment_NavHostFragment$onCreate(NavHostFragment.java:219)
    at androidx.navigation.fragment.NavHostFragment.onCreate(NavHostFragment.java)
    at androidx.fragment.app.Fragment.performCreate(Fragment.java:2949)
    at androidx.fragment.app.FragmentStateManager.create(FragmentStateManager.java:475)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:278)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2106)
    at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
    at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
    at androidx.fragment.app.FragmentContainerView.<init>(FragmentContainerView.java:180)
    at androidx.fragment.app.FragmentLayoutInflaterFactory.onCreateView(FragmentLayoutInflaterFactory.java:52)
    at androidx.fragment.app.FragmentController.onCreateView(FragmentController.java:135)
    at androidx.fragment.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:319)
    at androidx.fragment.app.FragmentActivity.onCreateView(FragmentActivity.java:298)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$tryCreateView(LayoutInflater.java:1067)
    at android.view.LayoutInflater.tryCreateView(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:995)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflate(LayoutInflater.java:1121)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:532)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$inflate(LayoutInflater.java:479)
    at android.view.LayoutInflater.inflate(LayoutInflater.java)
    at androidx.appcompat.app.AppCompatDelegateImpl.$$robo$$androidx_appcompat_app_AppCompatDelegateImpl$setContentView(AppCompatDelegateImpl.java:699)
    at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java)
    at androidx.appcompat.app.AppCompatActivity.$$robo$$androidx_appcompat_app_AppCompatActivity$setContentView(AppCompatActivity.java:195)
    at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java)
    at com.example.robolectrictest.MainActivity.onCreate(MainActivity.java:12)
    at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7995)
    at android.app.Activity.performCreate(Activity.java)
    at android.app.Activity.$$robo$$android_app_Activity$performCreate(Activity.java:7979)
    at android.app.Activity.performCreate(Activity.java)
    at android.app.Instrumentation.$$robo$$android_app_Instrumentation$callActivityOnCreate(Instrumentation.java:1309)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
    ... 21 more
Caused by: java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at org.robolectric.internal.bytecode.RobolectricInternals.performStaticInitialization(RobolectricInternals.java:61)
    at org.robolectric.internal.bytecode.ShadowWrangler.classInitializing(ShadowWrangler.java:163)
    ... 64 more
Caused by: java.lang.IllegalAccessError: failed to access class kotlin.jvm.internal.DefaultConstructorMarker from class androidx.navigation.NavController$Companion (kotlin.jvm.internal.DefaultConstructorMarker is in unnamed module of loader 'app'; androidx.navigation.NavController$Companion is in unnamed module of loader org.robolectric.internal.AndroidSandbox$SdkSandboxClassLoader @383bfa16)
    at androidx.navigation.NavController$Companion.<init>(NavController.kt)
    at androidx.navigation.NavController.__staticInitializer__(NavController.kt)
    ... 70 more

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.

// Jetpack Compose Integration
implementation "androidx.navigation:navigation-compose:2.4.0-alpha01"

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