Cannot find Symbol: ApplicationTestCase

Please Share On:

After I upgrade from Android 2.3 to Android X. I am facing “Cannot find Symbol: ApplicationTestCase”. As I am new in Android Studio, I had no idea how to fix this problem. If I faced any problem I always go to www.google.com and search for the solution.

Today, also I did the same and searched why I get “Cannot find Symbol: ApplicationTestCase”. I found a suggestion in www.stackoverflow.com. Delete two sub-folders of the .idea folder

  • ./idea/caches
  • ./idea/libraries

It will solve the problem. Yes, more than 20 users voted for solved problem but when I tried the same process, it didn’t. I have to apply another procedures.

Then, I decided, Why not I create another empty project. I have recently upgrade my AndroidX. It will definitely open my ApplicationText.java class and compare to the old version.

I create a new project named My Application.

Now, Go back and see old version, ApplicationTest.java

Now see new “Test.java” class

Here, I find out that “ApplicationTest.Java” class has been replace by “ExampleInstrumentTest.java”.

Firstly, I need to rename the name of old java class i.e ApplicationTest.Java into ExampleInstumentTest.Java.

Here is the process how to rename java class.

Right Click ApplicationTest.Java -> Refactor -> Rename -> ExampleInstumentTest

And Save.

See the below the renamed java class how it looks like after renamed.

Now, the time is to copy the code from newly created project to the old project.

Lastly, Don’t forget to change the package name into your project package name. Change “MyApplication” to “Your Package Name

Before

public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.geetaregmi.MyApplication", appContext.getPackageName());
    }
}

After

public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.geetaregmi.citizenshipTest", appContext.getPackageName());
    }
}

Now, add some dependency in your build.gradle(Module:app)

// Required for instrumented tests
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'

That’s it. It solved my problem. You can also try, if you are facing same problem like mine.



Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial