gpt4 book ai didi

android - java.lang.RuntimeException : Method isEmpty in android. text.TextUtils 未模拟

转载 作者:搜寻专家 更新时间:2023-11-01 09:25:38 27 4
gpt4 key购买 nike

loginUnit.java(测试文件)

@RunWith(MockitoJUnitRunner.class)
public class loginUnit {
@Mock
TextUtils mMockTextUtils;

@Test
public void checkStringEmpty(){

String empty = "";
when(mMockTextUtils.isEmpty(empty)).thenReturn(true);
assertThat(LoginActivity.isStringEmpty(empty),is(equalTo(true)));
}

来自 LoginActivity 的 UUT(被测单元)

public static boolean isStringEmpty(String s){
return TextUtils.isEmpty(s);
}

我总是得到 android.text.TextUtils not mocked 错误,请帮帮我?

最佳答案

在您的 src/test/java 文件夹中,添加 android.text 包并创建 TextUtils.java

public class TextUtils {
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
}

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

关于android - java.lang.RuntimeException : Method isEmpty in android. text.TextUtils 未模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50911878/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com