【问题标题】:How to run unit tests on methods in an Android activity?如何对 Android 活动中的方法运行单元测试?
【发布时间】:2020-04-24 03:48:36
【问题描述】:

在 Android Studio 中开发 Android 应用。我正在尝试为我的活动 LoginPage 中的方法编写单元测试。但是,我显然错过了一些大事。我认为我需要创建一个 LoginPage 实例并从该实例调用方法来测试它们。但是,当我尝试执行此操作时,每当我尝试访问该对象时都会收到 NullPointerException。如果我没有 Activity 的实例,如何测试这些特定于 Activity 的方法?我认为一种解决方案是将 Activity 中的每个数据字段都设为静态,但我觉得这会被认为是草率的编码。


import android.app.Activity;
import android.content.Context;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;

public class LoginPageTest {

    LoginPage myObjectUnderTest;


    public static final String FAKE_USERNAME = "rodneyram";


    @Before
    public void onLaunch() { myObjectUnderTest = new LoginPage();
    }


    @Test
    public void username_EditText_Test() {

        //Test if the usernameID editText widget correctly gets the written username.

        myObjectUnderTest.usernameID.setText("");
        myObjectUnderTest.usernameID.append(FAKE_USERNAME);
        String testString = myObjectUnderTest.usernameID.getText().toString();
        assertEquals(testString, FAKE_USERNAME);
    }

     @After
        public void tearDown() {
            myObjectUnderTest = null;
     }

}

我的错误信息:

java.lang.NullPointerException
    at com.example.donapp.LoginPageTest.password_EditText_Test(LoginPageTest.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) ``

【问题讨论】:

    标签: android android-studio testing android-activity junit


    【解决方案1】:

    您无法使用单元测试来测试 android API,但您需要进行工具 UI 测试。

    那是因为您需要访问您的活动和 UI 元素,例如在您的情况下为 EditText

    单元测试可以写在testjava包下,工具测试可以写在androidTest包下。

    为了在您的测试中获得一个活动实例,您需要使用@Rule 注解创建一个规则:

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

    这里我假设你的活动被命名为MainActivity

    然后您可以使用 Espresso ViewMatchers 访问 UI 小部件,然后您可以使用 ViewActions 对其应用操作。

    例如,检查您的usernameID EditText 文本字符串是否包含特定字符串:

    onView(withId(R.id.usernameID)).check(matches(withText(containsString("SOME_TEXT"))));
    

    演示仪器测试课

    @RunWith(AndroidJUnit4.class)
    public class EditTextTest {
    
        @Rule
        public ActivityTestRule mMainActivityRule = new ActivityTestRule<>(
                MainActivity.class);
    
        @Test
        public void editTextTest() {
           onView(withId(R.id.editText_main)).check(matches(withText(containsString("SOME_TEXT"))));
        }
    
    
    }
    
    

    查看documentation 以获得更多帮助。

    【讨论】:

      【解决方案2】:

      单元测试不是用来测试 UI 的,你通常用它们来测试一些业务逻辑。对于您的情况,您可以提取附加方法

      public String appendText(String textToAppend) {
       String userName = "";
       return userName.append(textToAppend);
      }
      

      然后为这个方法写一个单元测试:

      @Test
      public void appendTextTest() {
          String testString = appendText(FAKE_USERNAME);
          assertEquals(testString, FAKE_USERNAME);
      }
      

      如果您真的想测试 UI,可以考虑使用一些 UI 测试工具,例如 Espresso。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-29
        相关资源
        最近更新 更多