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不模拟错误,请帮助我?

最佳答案

在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

07-26 00:40