Android Studio中的单元测试会出错

时间:2018-06-07 11:10:13

标签: android unit-testing android-studio junit

我正在尝试为我的班级编写单元测试,但是我收到以下错误

java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked. See http://g.co/androidstudio/not-mocked for details.

at android.text.TextUtils.isEmpty(TextUtils.java)
at com.mvvm.testmvvm.Model.User.isValidUser(User.java:37)
at com.mvvm.testmvvm.LoginViewModelTest.isValidUser_Test(LoginViewModelTest.java:15)
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.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)
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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

这是我的班级

public class User {

    private String  mEmail;
    private String  mPassword;

    public User() {
    }

    public User(String mEmail, String mPassword) {
        this.mEmail = mEmail;
        this.mPassword = mPassword;
    }

    public String getEmail() {
        return mEmail;
    }

    public void setEmail(String mEmail) {
        this.mEmail = mEmail;
    }

    public String getPassword() {
        return mPassword;
    }

    public void setPassword(String mPassword) {
        this.mPassword = mPassword;
    }

    public int isValidUser () {

        if (TextUtils.isEmpty(getEmail()) == true)
            return 0;
        else if (!Patterns.EMAIL_ADDRESS.matcher(getEmail()).matches())
            return 1;
        else if (TextUtils.isEmpty(getPassword()) == true)
            return 2;
        else if (getPassword().length() < 6)
            return 3;
        else
            return -1;
    }
}

这是我的测试类

public class LoginViewModelTest {

    @Test
    public void isValidUser_Test() {

        User user = new User("a.elsayed2010@gmail.com", "amira123");
        int result = user.isValidUser();
        assertEquals(4, 2 + 2);
    }

}

有谁能告诉我这是什么问题?

修改

@Test
    public void isValidUser_happyCase() {

        User user = new User("a.elsayed2010@gmail.com", "amira123");
        int result = user.isValidUser();
        assertEquals(-1, result);
    }

    @Test
    public void isValidUser_emptyEmail() {

        User user = new User("", "amira123");
        int result = user.isValidUser();
        assertEquals(0, result);
    }

    @Test
    public void isValidUser_invalidEmail() {

        User user = new User("aaaaaaa", "amira123");
        int result = user.isValidUser();
        assertEquals(1, result);
    }

    @Test
    public void isValidUser_emptyPassword() {

        User user = new User("a.elsayed2010@gmail.com", "");
        int result = user.isValidUser();
        assertEquals(2, result);
    }

    @Test
    public void isValidUser_invalidPassword() {

        User user = new User("a.elsayed2010@gmail.com", "111");
        int result = user.isValidUser();
        assertEquals(3, result);
    }

1 个答案:

答案 0 :(得分:3)

您不能在本地JUnit测试中使用特定于Android框架的类(例如,在您的情况下为TextUtils),如果您启用unitTests.returnDefaultValues = true,它们仅作为存根提供。来自the documentation

  

如果您运行的测试调用Android SDK中没有模拟的API,则会收到错误消息,指出此方法未被模拟。这是因为用于运行单元测试的android.jar文件不包含任何实际代码(这些API仅由设备上的Android系统映像提供)。

您有三种选择:

  • 首先使用Robolectric测试库,它提供了在本地JUnit测试中运行的Android类的功能版本。这只需要在测试的顶部添加@RunWith(RobolectricTestRunner.class)

  • 其次是将测试作为Instrumented Unit Tests运行,方法是将测试类放在src/andoidTest文件夹下,并在实际设备或模拟器上运行测试。

  • 最后一个选项是创建自己的空安全实用程序方法,因为String.isEmpty()可以抛出NullPointerException并且Apache StringUtils库本身不可用于Android:

public static boolean isEmpty(@Nullable String in) {
    return in == null || in.isEmpty();
}

我建议您使用第一个选项,因为您只使用了实用程序类。