1一个简单的JUnit4测试

1一个简单的JUnit4测试

本文介绍了如何在运行Android的工作室1.1一个简单的JUnit4测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目,显示的Hello World。它是从Android的Studio中的空白活动模板创建的。

我然后添加/创建在我的应用程序包一个新的Java类(同一个包中有我的活动)。我称它的形状,并添加一个简单的构造

 公共类Shape {
    公共形状(int i)以{
        如果(我== 0){
            抛出新抛出:IllegalArgumentException(不能有0);
        }
    }
}
 

大。现在我有没有接触的Andr​​oid在所有的一类,我想单元测试。我应该怎么办?

这是我的问题停止。下面我会去通过什么我试过了。

请注意,我真的从来没有在Android或Java的测试。请原谅我菜鸟的错误。

  1. 而在Shape.java我去导航>测试
  2. 按回车键,选择创建新测试
  3. 在得到这个弹出,然后选择JUNIT4。
  • 在我然后点击修复按钮来修复该库未找到
  • 在我得到这个弹出
  • 我真的不知道该怎么选择,所以我选择默认/高亮显示。
  • 我写我的测试

     包com.eghdk.getjunit4towork;
    
    进口org.junit.Test;
    
    引入静态org.junit.Assert *。
    
    公共类ShapeTest {
        @Test(预期= IllegalArgumentException.class)
        公共无效testShapeWithInvalidArg(){
            新的Shape(0);
        }
    }
     

  • 在这一点上,我真的不知道怎么办好我的测试,但尽量要做到这一点:

  • 运行时,我得到这些错误

  • 解决方案

    由于Android工作室1.1,有(实验)的。从该页面有几个报价:

    要知道,有两种类型的测试是非常重要的: androidTest 和滑动测试

    • androidTest 主要是为运行在一个模拟器或设备的测试,如仪器仪表测试。在命令行中,使用 ./ gradlew connectedCheck 来运行这些。
    • 测试是你不希望在设备上运行测试,比如你写的单元测试。在运行 ./ gradlew测试来运行这些测试。

    由于在帖中说,你们之间的 androidTest 测试在Android的工作室通过更改检测工件进行切换。

    当然,这是pferred不运行设备或模拟器上测试$ P $,因为这加速了测试过程中的很多的。随着新的实验单元测试的支持,您可以访问存根的Andr​​oid的API,而无需使用的设备。这使您可以移动从 androidTest 更多的测试,以测试

    I have an Android project that shows "Hello World". It was created from the "Blank Activity" template from Android Studio.

    I then add/create a new java class in my application package (the same package that has my activity). I call it Shape and add a simple constructor

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

    Great. Now I have a class that isn't touching Android at all, and I want to unit test it. What should I do next?

    This is where my question stops. Below I'll go through what I tried.

    Please note that I really have never tested before in Android or Java. Excuse me for "rookie" mistakes.

    1. While in the Shape.java I go to "Navigate" > "Test"
    2. Hit enter to select "Create new Test"
    3. Get this popup, and select JUNIT4.
    1. I then hit the fix button to fix the library not being found
    2. I get this popup
    1. I'm not really sure what to select, so I select the default/highlighted.
    2. I write my test

      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. At this point, I'm not really sure how to run my tests, but try to do this:

    4. I get these errors when running

    解决方案

    Since Android Studio 1.1, there is (experimental) unit test support. A couple of quotes from that page:

    It is important to know that there are two test types: androidTest and plain test.

    • androidTest is primarily for tests you run on an emulator or device, such as instrumentation tests. From the command line, you use ./gradlew connectedCheck to run these.
    • test is for tests you don't want to run on a device, such as the unit test you wrote. You run ./gradlew test to run these tests.

    As stated in the quote, you switch between androidTest and test in Android Studio by changing the test artifact.

    Naturally, it is preferred to not run tests on a device or emulator, since this speeds up the testing process a lot. With the new experimental unit test support, you gain access to stubbed Android API's without using a device. This lets you move more tests from androidTest to test.

    这篇关于如何在运行Android的工作室1.1一个简单的JUnit4测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-03 20:04