【问题标题】:Error "Method isEmpty in android.text.TextUtils not mocked" when using PowerMockito.spy使用 PowerMockito.spy 时出现错误“android.text.TextUtils 中的方法 isEmpty 未模拟”
【发布时间】:2018-02-12 12:25:42
【问题描述】:

我在我的 Android 应用中使用 Mockito 和 PowerMock 进行本地单元测试:

testImplementation 'org.mockito:mockito-core:2.8.9'

String powerMockVersion = "1.7.3"
testImplementation "org.powermock:powermock-module-junit4:${powerMockVersion}"
testImplementation "org.powermock:powermock-api-mockito2:${powerMockVersion}"

这是我的代码:

MyTextUtils.java

package sample.com.sample_app;

import android.support.annotation.Nullable;
import android.text.TextUtils;

public class MyTextUtils {

    private MyTextUtils() {
        throw new AssertionError();
    }

    public static boolean isEmpty(@Nullable final CharSequence cs) {
        return TextUtils.isEmpty(cs);
    }
}

SystemUtils.java

package sample.com.sample_app;

import java.util.Random;

public class SystemUtils {

    private SystemUtils() {
        throw new AssertionError();
    }

    public static long currentTimeMillis() {
        return System.currentTimeMillis();
    }

    public static long nextLong() {
        return new Random().nextLong();
    }
}

还有我的测试。

FooTest.java

package sample.com.sample_app;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertEquals;

@RunWith(PowerMockRunner.class)
@PrepareForTest(SystemUtils.class)
public class FooTest {

    @Before
    public void setUp() {
        PowerMockito.spy(SystemUtils.class);
        PowerMockito.when(SystemUtils.currentTimeMillis()).thenReturn(1_000L);
    }

    @Test
    public void foo() {
        assertEquals(1_000L, SystemUtils.currentTimeMillis());
    }
}

LoremIpsumTest.java

package sample.com.sample_app;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import static org.junit.Assert.assertTrue;

@RunWith(PowerMockRunner.class)
@PrepareForTest(MyTextUtils.class)
public class LoremIpsumTest {

    @Before
    public void setUp() {
        PowerMockito.spy(MyTextUtils.class);
        PowerMockito.when(MyTextUtils.isEmpty("")).thenReturn(true);
    }

    @Test
    public void lorem() {
        assertTrue(MyTextUtils.isEmpty(""));
    }
}

当我运行LoremIpsumTest 时,它失败并出现以下错误:

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

如果我将PowerMockito.spy(MyTextUtils.class); 替换为PowerMockito.mockStatic(MyTextUtils.class);LoremIpsumTest 有效。

另一方面,FooTestPowerMockito.spy(SystemUtils.class); 一起使用。 LoremIpsumTest 中的部分 mocking 有什么问题?

【问题讨论】:

    标签: android unit-testing mockito powermock


    【解决方案1】:

    http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-

    “我们知道默认行为在使用 Log 或 TextUtils 等类时会出现问题,并将在未来的版本中评估可能的解决方案。”

    https://medium.com/@okmanideep/dont-create-that-stringutils-to-unit-test-your-android-class-8ab32af34e84

    【讨论】:

      猜你喜欢
      • 2023-01-14
      • 2019-10-29
      • 2015-12-12
      • 2020-06-05
      • 2018-02-21
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多