Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio Integration test cannot resolve symbol AndroidJUnit4

I've seen partial answers to similiar issues but never one that actually solved the problem. I've boiled this down to a minimal app to demonstrate the issue.

I'm using the most current version of Android Studio (V2.2.1) I have the following SDK's installed:

  • Android SDK Tools v25.1.6
  • Android SDK Platform-tools v23.1
  • Android SDK Build-tools v23.0.3
  • Android Support repository v32

    1. Create new Android app using wizard
    2. Updated build.gradle (app) per: https://developer.android.com/training/testing/start/index.html#config-instrumented-tests

      • Added under defaultConfig:

        • testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
      • Added under dependencies

        • androidTestCompile 'com.android.support:support-annotations:23.0.1' <-- Changed to 23.4.0 to match newly generated app
        • androidTestCompile 'com.android.support.test:runner:0.4.1'
        • androidTestCompile 'com.android.support.test:rules:0.4.1'
    3. Did project sync to sync Gradle

    4. Under the "test" package add new test file - ExampleIntegrationTest.java (right next to ExampleUnitTest.java)

    5. In the new file, created integration test per: https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html


import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.SmallTest;

import org.junit.runner.RunWith;


@RunWith(AndroidJUnit4.class)
@SmallTest
public interface ExampleIntegrationTest {
}

This is where the problem comes up - cannot resolve symbol AndroidJUnit4 On the line: import android.support.test.runner.AndroidJUnit4;

If I type the import statement I can use the popup suggestions:
- import android. - It shows "support as option"
- import android.support. - It shows "test" as an option
- import android.support.test. - Now it only shows "rule" as a option

After I create the app I make sure it's using the "debug" variant

This is pretty cut and dry. As far as I can tell this should work,, but the support library for some reason isn't getting imported correctly.

like image 541
user3786170 Avatar asked May 31 '16 18:05

user3786170


2 Answers

Fix this by adding the test to the

  • /app/src/androidTest Directory instead of the
  • /app/src/test Directory which is for unit tests only

Or, when you create the test class, add the test by selecting new class when in "Android Instrumentation Tests" tab of the Project tree

like image 121
scoleman2272 Avatar answered Nov 14 '22 11:11

scoleman2272


Please ensure you have your tests located at the right folder. There are local and instrumentation tests which must be resided in their respective folders as explained below:

Take a look at this article which says...

Run Instrumented Unit Tests To run your instrumented tests, follow these steps:

Be sure your project is synchronized with Gradle by clicking Sync Project in the toolbar. Run your test in one of the following ways: To run a single test, open the Project window, and then right-click a test and click Run . To test all methods in a class, right-click a class or method in the test file and click Run . To run all tests in a directory, right-click on the directory and select Run tests . The Android Plugin for Gradle compiles the instrumented test code located in the default directory (src/androidTest/java/), builds a test APK and production APK, installs both APKs on the connected device or emulator, and runs the tests. Android Studio then displays the results of the instrumented test execution in the Run window.

Therefore folks, for instrumentation test the folder must be (do not forget the case)

src/androidTest/java

and for local tests the folder must be

src/test/java

You can then have your package folder(s) to match your app package

Hope, this helps for the community!

like image 38
Vincy Avatar answered Nov 14 '22 10:11

Vincy