繁体   English   中英

如何对 Android 活动中的方法运行单元测试?

[英]How to run unit tests on methods in an Android activity?

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


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 API,但您需要进行工具 UI 测试。

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

单元测试可以写在test java package下,仪器测试可以写在androidTest package下。

为了在测试中获取活动实例,您需要使用 @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"))));

演示仪器测试class

@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"))));
    }


}

检查文档以获取更多帮助。

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

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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM