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没有模拟错误,请帮帮我?
答案 0 :(得分:2)
在你的src / test / java文件夹中,添加android.text包并创建TextUtils.java
public class TextUtils {
public static boolean isEmpty(CharSequence str) {
return str == null || str.length() == 0;
}
}
答案 1 :(得分:0)
见http://tools.android.com/tech-docs/unit-testing-support#TOC-Method-...-not-mocked.-
android {
// ...
testOptions {
unitTests.returnDefaultValues = true
}
}