Cannot resolve AndroidJUnit4 character

I am trying to add loginfacebook for my application. But when I added the repository, which is needed for this. This caused an error. 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 assembly: gradle (application)

 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' 
+8
source share
6 answers

Try

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

Add the following dependency sections

 configurations.all { resolutionStrategy.force 'com.android.support:support-annotations:23.1.0' } 
+8
source

In addition to the answer above, please make sure that you strictly follow these steps:

I banged my head against a wall and / or any object that I see in front of me to check my SQLite function block. No matter what I do, how strictly follow the advice, provided that my many wise people across the Internet have not worked.

Then I had to go to a preschool and start again to realize my stupid mistake . I found out that AndroidJUnit4 can only be used with Instrumentation Test, and JUnit should be used for local tests. In this case, the folder should be src / androidTest / java . I had a test class right in the androidTest folder, so I had to deal with this nasty bug. However, the moment I moved it to src / androidTest / java, everything became very clear: "I clearly see that the rain has passed."

Take a look at this article that says ...

Run Instrumented Unit Tests Follow these steps to run your instrumental tests:

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

Therefore, guys, for instrumentation, the folder should be (do not forget the case)

SRC / androidTest / Java

and for local tests the folder should be

Src / test / java

After that, you can have your own folders for packages that match your application package.

Hope this helps the community!

+7
source

Another important thing (which showed me this problem) is to check if you have changed the type of assembly type for the tests - this is the testBuildType parameter in your build.gradle module.
If you changed it (like me), then before editing any tests on Android:

  • Go to the β€œBuild / Flavor Android” tab.
  • Select the same assembly type that you specified in the testBuildType Gradle property
  • Sync Android Studio with Gradle

To start the application again, you obviously need to go back to "debug", I suppose.

Note. Although this issue fixes my underlying issue as described in this question, I'm still looking for a way to support custom build types for Android tests, but it also allows me to create debug types; my ultimate goal is to run CI tests with a special build type, but let the developers run them in debug mode. Therefore, if anyone has an idea on how to do this, the comment section is yours. :)

+2
source

Just add compile "com.android.support.test:runner:1.0.1" to your Gradle.

0
source

For me, this was because some of the androidTestImplementation imports androidTestImplementation not updated in build.gradle. After I updated them to the latest version, the error disappeared.

Remarks:

There were several modules in my project, and I had to update them in each module.

0
source

Please try the latest API as shown below.

 testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-ore:3.0.2' 

However, no luck, then try the "implementation" instead of testImplementation, androidTestImplementation and androidTestImplementation and synchronize in Gradle.

0
source

Source: https://habr.com/ru/post/1266581/


All Articles