java - 如何在 Android Studio 1.1 中运行一个简单的 JUnit4 测试?

标签 java android unit-testing junit android-studio

我有一个显示“Hello World”的 Android 项目。它是从 Android Studio 的“空白 Activity ”模板创建的。

然后我在我的应用程序包(与我的 Activity 相同的包)中添加/创建一个新的 java 类。我称它为 Shape 并添加一个简单的构造函数

public class Shape {
    public Shape(int i){
        if (i==0){
            throw new IllegalArgumentException("Cant have 0");
        }
    }
}

太好了。现在我有一个完全不接触 Android 的类(class),我想对它进行单元测试。接下来我该怎么做?

我的问题到此为止。下面我将介绍我尝试过的内容。

请注意,我以前从未在 Android 或 Java 中进行过测试。请原谅我的“新手”错误。

  1. 在 Shape.java 中,我转到“导航”>“测试”
  2. 按回车键选择“创建新测试”
  3. 获取此弹出窗口,然后选择 JUNIT4。

enter image description here

  1. 然后我点击修复按钮来修复找不到的库
  2. 我收到了这个弹出窗口

enter image description here

  1. 我不确定要选择什么,所以我选择了默认/突出显示。
  2. 我写我的测试

    package com.eghdk.getjunit4towork;
    
    import org.junit.Test;
    
    import static org.junit.Assert.*;
    
    public class ShapeTest {
        @Test(expected = IllegalArgumentException.class)
        public void testShapeWithInvalidArg() {
            new Shape(0);
        }
    }
    
  3. 此时,我不确定如何运行我的测试,但尝试这样做: enter image description here

  4. 运行时出现这些错误

    Error:(3, 17) Gradle: error: package org.junit does not exist
    Error:(5, 24) Gradle: error: package org.junit does not exist
    Error:(8, 6) Gradle: error: cannot find symbol class Test

最佳答案

从 Android Studio 1.1 开始,有(实验性)unit test support .该页面的一些引用:

You will have to specify your testing dependencies in the build.gradle file of your android module. For example:

dependencies {
  testCompile 'junit:junit:4.12'
  testCompile "org.mockito:mockito-core:1.9.5"
}

To use unit testing support in AS, you have to do the following steps:

  1. Update build.gradle to use the android gradle plugin version 1.1.0-rc1 or later (either manually in build.gradle file or in the UI in File > Project Structure)

  2. Add necessary testing dependencies to app/build.gradle (see above).

  3. Enable the unit testing feature in Settings > Gradle > Experimental.

  4. Sync your project.

  5. Open the "Build variants" tool window (on the left) and change the test artifact to "Unit tests".

  6. Create a directory for your testing source code, i.e. src/test/java. You can do this from the command line or using the Project view in the Project tool window. The new directory should be highlighted in green at this point. Note: names of the test source directories are determined by the gradle plugin based on a convention.

  7. Create your test. You can do this by opening a class, right-clicking its name and selecting "Go to > Test". Add some test cases.
  8. Right click your new test class or method and select "Run ...".
  9. (Optional) You can decrease the compilation time by using Gradle directly. To do this, go to the Run menu and select "Edit configurations". There, find the default JUnit template, remove the "Make" before-launch step and add a "Gradle aware make" step instead (leave the task name empty).

重要的是要知道有两种测试类型:androidTest 和普通 test

  • androidTest 主要用于您在模拟器或设备上运行的测试,例如仪器测试。在命令行中,您使用 ./gradlew connectedCheck 来运行这些。
  • test 用于您不想在设备上运行的测试,例如您编写的单元测试。您运行 ./gradlew test 来运行这些测试。

如引用中所述,您可以通过更改测试 Artifact 在 Android Studio 中的 androidTesttest 之间切换。

当然,最好不要在设备或模拟器上运行测试,因为这会大大加快测试过程。借助新的实验性单元测试支持,您无需使用设备即可访问 stub Android API。这使您可以将更多测试从 androidTest 移动到 test

关于java - 如何在 Android Studio 1.1 中运行一个简单的 JUnit4 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28843304/

相关文章:

android - Google Play服务版本过时会不会让应用无法运行?

java - EasyMock 给出意想不到的结果,表示预期为 1,实际为 0

firebase - 使用 firebase-functions-test、mocha 和 Emulator Suite 对 Firebase(云)函数进行单元测试时,为什么无法访问 .env 变量?

reactjs - 类型错误 : Cannot read property 'location' of undefined (jest)

java - 在 JDialog 中使用 JavaFX JFXPanel 时无法看到视频

java - PostgreSQL 错误 : canceling statement due to user request

java - 从 Objective C 到 java 的 SQLite 查询

android - Android Studio项目结构中的 "com.example.helloWorld(androidTest)"包是什么

java - 如何使用主类中的方法从嵌套类返回泛型类型

java - 在android中调用soap webservice