7

I'm getting this error while trying to use FragmentScenario in Android

error: package androidx.fragment.app.testing does not exist
import androidx.fragment.app.testing.FragmentScenario;
                                    ^

This is my simple Unit test example:

package com.example.myapplication;

import androidx.fragment.app.testing.FragmentScenario;
import android.os.Build;

import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;

@RunWith(AndroidJUnit4.class)
@Config(manifest = Config.NONE, sdk = Build.VERSION_CODES.P)
public class MainActivityTest {
    @Test
    public void testFragmentScenario() {
        FragmentScenario<BlankFragment> scenario = FragmentScenario.launchInContainer(BlankFragment.class);
    }
}

build.gradle(app):

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    def test_version = '1.2.0'
    def fragment_version = '1.2.4'
    debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
    testImplementation "androidx.test:core:$test_version"
    testImplementation 'androidx.test.ext:junit:1.1.1'
    testImplementation "androidx.test:runner:$test_version"
    testImplementation "androidx.test:rules:$test_version"
    testImplementation 'org.robolectric:robolectric:4.3'
}

What am I missing because a few months ago this piece of code works.

3
  • did you ever resolve this? Commented Jun 26, 2020 at 13:30
  • For what it's worth, my dependencies are near identical to yours and the androidx.fragment.app.testing.FragmentScenario import resolves for me. You might just need to resync your project with the Gradle files? Commented Aug 14, 2020 at 9:29
  • Hi @StealthRabbi sorry for the late reply. I haven't tried it since and moved on to other things. Let me try it soon and update here. Commented Aug 18, 2020 at 3:47

2 Answers 2

2

Check your build variants in Android Studio. It should be debug

Sign up to request clarification or add additional context in comments.

Comments

0

It looks that you use the wrong way, try to change debugImplementation to testImplementation in build.gradle. Hope it will helpful to you.

1 Comment

This is not possible. androidx.fragment:fragment-testing only allows debugImplementation

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.