Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resolve symbol AndroidJUnit4

I'm trying to add loginfacebook for my app. But when I added a repository that is need in doing this. It caused an error. The AndroidJUnit4 cannot resolve now.

ExampleInstrumentedTest.java

package com.example.user.enyatravelbataan;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static junit.framework.Assert.assertEquals;
import static org.junit.Assert.*;

/**
 * Instrumentation test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
    // Context of the app under test.
    Context appContext = InstrumentationRegistry.getTargetContext();

    assertEquals("com.example.user.enyatravelbataan", 
appContext.getPackageName());
}
}

and this is my build:gradle(app)

apply plugin: 'com.android.application'

 android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
useLibrary 'org.apache.http.legacy'


repositories {
    mavenCentral()
}

defaultConfig {
    applicationId "com.example.user.enyatravelbataan"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile(name: 'wikitudesdk', ext: 'aar')
// compile 'org.apache.httpcomponents:httpclient:4.5'
// compile 'org.apache.httpcomponents:httpcore:4.4.1'
compile files('libs/MD5Simply.jar')
compile files('libs/GenAsync.1.2.jar')
compile 'com.facebook.android:facebook-android-sdk:[4,5)'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.android.gms:play-services:9.8.0'
compile 'com.google.android.gms:play-services-location:9.8.0'
compile 'com.google.android.gms:play-services-appindexing:9.8.0'
compile 'com.android.support:cardview-v7:24.2.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support:design:24.2.1'
compile 'com.android.volley:volley:1.0.0'
compile 'com.android.support:support-v4:24.2.1'

testCompile 'junit:junit:4.12'
}

repositories {
flatDir {
    dirs 'libs'
}
}
apply plugin: 'com.google.gms.google-services'
like image 849
Meng Avatar asked Apr 11 '17 10:04

Meng


4 Answers

Try

androidTestImplementation 'com.android.support:support-annotations:23.1.0'
androidTestImplementation 'com.android.support.test:runner:0.4.1'
androidTestImplementation 'com.android.support.test:rules:0.4.1'

Add following above dependencies section

configurations.all {
   resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'
}
like image 142
PEHLAJ Avatar answered Oct 11 '22 10:10

PEHLAJ


In addition to the above answer please ensure you follow these steps strictly:

I was banging my head against the wall and/or any object i see in front of me in order to make my SQLite function unit tested. No matter what I do, how strictly follow the suggestions provided my many wise people all over the internet did not work.

I then had to go to preschool and start over to realize my stupid mistake. I learned that AndroidJUnit4 can only be used with Instrumentation Test and JUnit must be used for local tests. That being said, the folder must be src/androidTest/java. I had my test class directly under androidTest folder, hence I had to face that nasty error. However, the moment I moved it under src/androidTest/java everything went very clear like "I can see clearly now the rain is gone".

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 27
Vincy Avatar answered Oct 11 '22 10:10

Vincy


Another important thing (that has surfaced this issue for me) is to check if you have changed the build type for tests - that would be the testBuildType option in your module's build.gradle.
If you did change it (like I have), then before editing any Android tests:

  1. Go to your Android Studio build/flavors tab
  2. Select the same build type you specified in your testBuildType Gradle property
  3. Sync Android Studio with Gradle

To run the app normally again, you would obviously need to switch back to 'debug' I suppose.

Note: Even though this fixes my core issue as described by the question here, I'm still looking for a way to support both custom build types for Android tests, but also allow debug build types; my ultimate goal is to have CI running the tests with a special build type, but let developers run them in 'debug' mode. So if anyone has an idea on how to accomplish that, the comments section is yours. :)

like image 2
milosmns Avatar answered Oct 11 '22 11:10

milosmns


If this issue is coming after migrating to AndroidX. Do this:-

1. Correct imports

import androidx.test.runner.AndroidJUnit4

instead of

import android.support.test.runner.AndroidJUnit4

2. Correct method

Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

instead of

Context appContext = InstrumentationRegistry.getTargetContext()
like image 1
bikram Avatar answered Oct 11 '22 12:10

bikram