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

Ian*_*lla 1 testing junit android android-activity android-studio

在 Android Studio 中开发 Android 应用。我正在尝试为我的活动 LoginPage 中的方法编写单元测试。然而,我显然错过了一些大事。我认为我需要创建一个 LoginPage 实例并调用该实例中的方法来测试它们。但是,当我尝试执行此操作时,每当我尝试访问该对象时,都会收到 NullPointerException。如果我没有 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;
     }

}
Run Code Online (Sandbox Code Playgroud)

我的错误信息:

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) ``
Run Code Online (Sandbox Code Playgroud)

Zai*_*ain 7

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

这是因为您需要访问您的 Activity 和 UI 元素,例如您的情况中的EditText.

单元测试可以写在testjava包下,仪器测试可以写在androidTest包下。

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

@Rule
public ActivityTestRule mMainActivityRule = new ActivityTestRule<>(
        MainActivity.class);
Run Code Online (Sandbox Code Playgroud)

在这里我假设您的活动名为MainActivity

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

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

onView(withId(R.id.usernameID)).check(matches(withText(containsString("SOME_TEXT"))));
Run Code Online (Sandbox Code Playgroud)

演示乐器测试课程

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


}

Run Code Online (Sandbox Code Playgroud)

检查文档以获得进一步的帮助。