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

问题描述 投票:68回答:8

我在导入某些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()));
  }
}

但是由于某些原因,出现错误'找不到符号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'

所以我想我已经完成了所有依赖项设置-我已经尝试了很多事情,但是没有运气。

有人有什么想法吗?

android android-support-library junit4 android-espresso
8个回答
61
投票

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

单元测试

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

  • 它们在Android模拟器上运行,并具有对所有Android类的完全访问权限
  • 它们被保存在androidTest/java包中>
  • 需要在androidTestCompile的build.gradle中添加依赖项>
  • 您通常将Espresso和JUnit用于这些测试
  • 据我所知,您正在尝试使用Espresso编写仪器测试,但是将您的测试放在用于单元测试的test/java包中。在这种情况下,您需要将测试类移至androidTest/java包。

    在新版本中添加这些:

    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    

    如果您迁移到AndroidX,请使用此:

    androidTestImplementation 'androidx.test:rules:1.1.1'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    

    添加:

    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    

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

    需要此添加依赖项

     testCompile 'com.android.support.test:rules:0.5'
     testCompile 'com.android.support.test:runner:0.5'
    

    添加依赖项。

    androidTestCompile 'com.android.support.test:rules:0.5'
    androidTestCompile 'com.android.support.test:runner:0.5'
    

    在Android中编写UI测试

    (((在Android设备/仿真器上运行的测试)),请确保
    1. 测试类位于androidTest

    包中,而不是test包中。
  • 请确保您在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'

  • 对于单元测试

    (确保在JVM上运行的测试)>

    1.Test类在test

    包中

    2。确保在build.gradle中进行以下依赖

testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:2.23.0'
 

从androidX使用

androidTestImplementation'androidx.test:rules:1.1.1'

androidTestImplementation'androidx.test:runner:1.1.1'

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

例如:

>     dependencies {
>     
>         androidTestImplementation 'androidx.test:rules:1.1.1'
>         androidTestImplementation 'androidx.test:runner:1.1.1'
>     }

然后导入

import androidx.test.rule.ActivityTestRule;


119
投票

在新版本中添加这些:

androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'

48
投票

如果您迁移到AndroidX,请使用此:

androidTestImplementation 'androidx.test:rules:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.1'

34
投票

添加:

androidTestImplementation 'com.android.support.test:rules:1.0.2'

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


22
投票

需要此添加依赖项

 testCompile 'com.android.support.test:rules:0.5'
 testCompile 'com.android.support.test:runner:0.5'

10
投票

添加依赖项。

androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test:runner:0.5'

3
投票

在Android中编写UI测试

(((在Android设备/仿真器上运行的测试)),请确保
  1. 测试类位于androidTest


2
投票

从androidX使用

androidTestImplementation'androidx.test:rules:1.1.1'

androidTestImplementation'androidx.test:runner:1.1.1'

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

© www.soinside.com 2019 - 2024. All rights reserved.