为什么我不能将AndroidJUnit4和ActivityTestRule导入我的单元测试类?

Hal*_*upa 64 android junit4 android-support-library android-espresso

我在导入一些Android UI测试框架时遇到了麻烦 - 我无法弄清楚出了什么问题!

这是我的班级:

@RunWith(AndroidJUnit4.class)
@LargeTest
public class ExampleUnitTest {

@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule(MainActivity.class);

@Test
public void listGoesOverTheFold() {
    onView(withText("Hello world!")).check(matches(isDisplayed()));
  }
}
Run Code Online (Sandbox Code Playgroud)

但由于某种原因,我得到错误'找不到符号ActivityTestRule'和'找不到符号AndroidJUnit4'.我试图导入它们但是找不到它们.

build.gradle中的依赖项设置为:

compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
androidTestCompile 'com.android.support:support-annotations:23.4.0'

androidTestCompile 'com.android.support.test:runner:0.4'
androidTestCompile 'com.android.support.test:rules:0.4'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
Run Code Online (Sandbox Code Playgroud)

所以我认为我已经设置了所有依赖项 - 我一直在尝试很多东西,但没有运气.

有人有主意吗?

小智 116

在较新版本中添加以下内容:

androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
Run Code Online (Sandbox Code Playgroud)


Jah*_*old 56

您可以在Android中设置两种不同类型的测试

单元测试

  • 它们直接在JVM上运行,无法访问Android框架类.
  • 它们保存在test/java包装中
  • 需要使用该命令在build.gradle文件中添加依赖项 testCompile
  • 您通常使用Mockito,Robolectric和JUnit进行这些测试

仪器测试

  • 它们在Android模拟器上运行,并且可以完全访问所有Android类
  • 它们保存在androidTest/java包装中
  • 需要将依赖项添加到build.gradle中 androidTestCompile
  • 您通常使用Espresso和JUnit进行这些测试

据我所知,你正在尝试用Espresso编写仪器测试,但是在test/java用于单元测试的包中进行测试.在这种情况下,您需要将测试类移动到androidTest/java包中.

  • 谢谢,这解决了!我不知道有测试/ java和androidTest/JAVA之间的差异 (3认同)

per*_*ser 32

如果您迁移到AndroidX,请使用以下命令:

androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.1'
Run Code Online (Sandbox Code Playgroud)


小智 29

添加:

androidTestImplementation 'com.android.support.test:rules:1.0.2'
Run Code Online (Sandbox Code Playgroud)

解决问题,但不要忘记将项目与gradle文件同步.只有这样,更改才会生效.


小智 21

需要这个添加依赖项

 testCompile 'com.android.support.test:rules:0.5'
 testCompile 'com.android.support.test:runner:0.5'
Run Code Online (Sandbox Code Playgroud)


Vin*_*ohn 12

从 androidX 使用

androidTestImplementation 'androidx.test:rules:1.1.1'

androidTestImplementation 'androidx.test:runner:1.1.1'
Run Code Online (Sandbox Code Playgroud)

在您的应用程序级别 build.gradle 文件的依赖项部分

例如:

 dependencies {

     androidTestImplementation 'androidx.test:rules:1.1.1'
     androidTestImplementation 'androidx.test:runner:1.1.1'
 }
Run Code Online (Sandbox Code Playgroud)

然后导入

导入 androidx.test.rule.ActivityTestRule;


小智 9

添加依赖项.

androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'
Run Code Online (Sandbox Code Playgroud)


Ami*_*pta 6

要在 Android 中编写UI 测试((在 Android 设备/模拟器上运行的测试)),请确保

  1. Test 类在androidTest包中,而不是在test包中。

  2. 确保在 build.gradle 中创建以下依赖项

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
testImplementation 'junit:junit:4.13'
Run Code Online (Sandbox Code Playgroud)

对于单元测试(在 JVM 上运行的测试),请确保

1.测试类在测试包中

2.确保在build.gradle中做了如下依赖

testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:2.23.0'
Run Code Online (Sandbox Code Playgroud)