IT

'AndroidJUnit4'기호를 확인할 수 없습니다

lottoking 2020. 7. 3. 18:07
반응형

'AndroidJUnit4'기호를 확인할 수 없습니다


분명히이 문제를 해결하려면 올바른 가져 오기 명령문이 필요합니다. 에 대한 문서에AndroidJUnit4 따르면 이것은

import android.support.test.runner.AndroidJUnit4;

그렇게하면 Android Studio runner가 빨간색으로 강조 표시 되고 "기호 '러너'를 해결할 수 없습니다"라고 불평합니다.

배경

UI Automator를 사용하여 테스트설정하기 위해 Android 개발자 사이트의 학습서를 따라이 시점에 도달했습니다 . 발생한 첫 번째 문제의 그 것이었다 com.android.support:support-v4:22.2.0com.android.support.test:runner:0.2의 다른 버전에 따라 달라집니다 com.android.support:support-annotations. 이 Android 버그 보고서 의 제안을 따르고 allprojects프로젝트에 다음을 추가했습니다 build.gradle.

configurations.all {
    resolutionStrategy.force 'com.android.support:support-annotations:22.1.0'
}

이것은 즉각적인 오류를 해결했지만 현재의 문제로 이어질 것으로 생각됩니다. 누구 든지이 문제를 해결하는 방법에 대한 제안이 있습니까?

`./gradlew : app : dependencies의 관련 섹션

androidTestCompile - Classpath for compiling the androidTest sources.
+--- com.jayway.android.robotium:robotium-solo:5.2.1
+--- com.squareup:fest-android:1.0.8
|    \--- org.easytesting:fest-assert-core:2.0M10
|         \--- org.easytesting:fest-util:1.2.5
+--- com.android.support.test:runner:0.2
|    +--- junit:junit-dep:4.10
|    |    \--- org.hamcrest:hamcrest-core:1.1
|    +--- com.android.support.test:exposed-instrumentation-api-publish:0.2
|    \--- com.android.support:support-annotations:22.0.0 -> 22.2.0
+--- com.android.support.test:rules:0.2
|    \--- com.android.support.test:runner:0.2 (*)
\--- com.android.support.test.uiautomator:uiautomator-v18:2.1.0

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:22.2.0
|    \--- com.android.support:support-v4:22.2.0
|         \--- com.android.support:support-annotations:22.2.0
+--- com.android.support:support-v4:22.2.0 (*)
+--- com.google.android.gms:play-services:6.1.71
|    \--- com.android.support:support-v4:20.0.0 -> 22.2.0 (*)
+--- com.crashlytics.android:crashlytics:1.+ -> 1.1.13
\--- com.jakewharton:butterknife:5.1.2

디버그 빌드 변형에서 앱을 확인하십시오. 빌드> 빌드 변형 선택 ...으로 이동 하면 다음이 표시됩니다.

여기에 이미지 설명을 입력하십시오


테스트 클래스를 src / test 에 넣는 실수를 저질렀습니다 . 그것들을 src / androidTest / java /로 옮긴 후 의존성이 해결되었습니다.


자 여기 실수와 내 것이 있습니다!

우리가 코드 파이스 쓰기 위하여려고하는 경우에 지역 단위 테스트 우리가 사용하지 말아야 @RunWith(AndroidJUnit4.class)AndroidJUnit4를 사용하지 않는 우리 원인을하지만 우리는 Junit4이 필요합니다. 그래서 우리는 작성해야합니다 @RunWith(JUnit4.class). 물론 Java 테스트 파일은 app/src/test/java/your.package.name디렉토리 아래에 있습니다.

그렇지 않으면 (!!) 우리는 Android Instrumented Unit Test 를 작성하려면 테스트 java 파일을 app/src/androidTest/java/your.package.name디렉토리에 넣고 주석을 사용해야합니다.@RunWith(AndroidJUnit4.class)


최신 정보

Android 테스트 라이브러리는 이제 AndroidX의 일부입니다. 공식 문서 에있는 올바른 Gradle 종속성을 사용하십시오 .

원래 답변

여기에서 내가 사용 하는 것보다 최신 버전의 테스팅 지원 라이브러리가 있음을 발견했습니다.

dependencies {
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}

참고 :이 라이브러리의 최신 버전을 사용해야합니다. 이 질문은 Android 테스트 지원 라이브러리가 새로 만들어졌고 여기에있는 버전 번호가 매우 오래된 시간입니다.


앱의 build.gradle 파일을 약간 변경하여 문제를 해결했습니다. 에서 dependencies { ... }섹션에서 다음 줄을 포함해야합니다 :

debugImplementation 'com.android.support.test:runner:1.0.1'

or whatever version is the newest at that point (...Compile is deprecated and has been replaced by ...Implementation). Note the use of debugImplementation. Android Studio suggested auto-including it with androidTestImplementation, which didn't work.

I found out how to change it from test to debug by looking in Project Structure under Dependencies of the app module, where you can change the scope of each dependency, see below.

프로젝트 구조


In my case this helped for release variant:

android {
    ...
    testBuildType "release" 
}

If anyone still having this issue:

Cannot resolve symbol 'AndroidJUnit4'

and using API 27, in the build.gradle which is in the app module, add the following lines:

testImplementation 'junit:junit:4.12'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'

// Espresso dependencies
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

Notice that this the OP is now in 2019 , 4 years old so If you are using Android X then AndroidJUnit4.class is deprecated , you have an error there and one more with this androidx.test.ext.junit.runners.AndroidJUnit4. I suggest to read this links to solve the problem .

AndroidJUnit4.class is deprecated: How to use androidx.test.ext.junit.runners.AndroidJUnit4?

Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'? For me Android Studio suggested to replace

@RunWith(AndroidJUnit4.class)

which was deprecated with

@RunWith(AndroidJUnit4ClassRunner.class)

and this

androidx.test.ext.junit.runners.AndroidJUnit4

with this

import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner;

After that the error is gone but I don't know if the future test while run ok ?!


put this code in your Dependencies

compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

If you're using project with multiple build-type then the selected build-type in build-variant window must be mentioned with testBuildType tag in module's build.gradle file.

For e.g.: If you're using build type debug then you should add android{testBuildType "debug" }, if using stage then add android{testBuildType "stage"} statement in android tag.


Move the test class to src/androidTest/java/. Then dependency will resolve.


Adding

compile com.android.support.test:runner:0.5'

resolved this exact issue for me.


As the list of answers demonstrate, this can be caused by a few things. One more for the list:

I ran an over-zealous LINT which removed all unused imports. This will produce the same errors, and it is easy to miss that this is the problem.

Android-studio will highlight references that are missing in the test code - and the ALT-ENTER popup will appear (this is the bit that is easy to miss).

Next, I need to remove the tests from LINT - or at least disable this warning.

Edit: @Code-Apprentice, the lines that were missing were:

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;


import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

따라서 파일의 첫 번째 오류는 @RunWith(AndroidJUnit4.class)테스트 클래스가 시작될 때 발생했습니다 .


고전적인 Invalidate Caches / Restart 가 도움이되었습니다! :)


build.gradle 파일에서이 종속성을 추가하십시오.

androidTestImplementation 'androidx.test.ext:junit:1.1.1'

최종 버전 ( 1.1.1)을 최신 버전으로 업데이트하십시오 .

참고 URL : https://stackoverflow.com/questions/30603487/cannot-resolve-symbol-androidjunit4

반응형