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

问题描述 投票:0回答:1

我在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个回答
0
投票

你不能用单元测试来测试Android API,而是需要做工具性的UI测试。

这是因为你需要访问你的活动和UI元素,就像你的例子中的 EditText.

单元测试可以写在 test java包,工具测试可以在 androidTest 包。

为了在你的测试中获得一个活动实例,你需要使用 @Rule 注释创建一个 Rule。

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

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

然后,您可以使用Espresso访问用户界面小部件。ViewMatchers 然后您可以使用 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"))));
    }


}

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


1
投票

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

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。

© www.soinside.com 2019 - 2024. All rights reserved.