Android单元测试问题解决

1.'java.lang.RuntimeException: Method isEmpty in android.text.TextUtils not mocked'报错

https://www.jianshu.com/p/f5d197a4d83a

2.利用反射测试native方法

//测试私有方法
@Test
public void isLoginResultValidTest() throws Exception {
LoginResultModel result = new LoginResultModel();
result.uid = 722264821;
result.session = " ";
result.first_login = false;
result.new_add = false;
result.phone = "13269964826";
result.secret = "";
result.is_new_device = false;
result.isVisitor = false;
System.out.println(result.toString());

//获取目标类的class对象
// Class<LoginServiceImpl> clazz = LoginServiceImpl.class;
// Method declaredMethod = clazz.getDeclaredMethod("isLoginResultValid", boolean.class);
// declaredMethod.setAccessible(true);
// Object invoke = declaredMethod.invoke(loginServiceImpl, result);
// declaredMethod.setAccessible(false);
Class<LoginServiceImpl> class1 = LoginServiceImpl.class;
//获取目标类的实例
Object instance = class1.newInstance();
//getDeclaredMethod() 可获取 公共、保护、默认(包)访问和私有方法,但不包括继承的方法。
//getMethod() 只可获取公共的方法
Method method = class1.getDeclaredMethod("isLoginResultValid",LoginResultModel.class);
//值为true时 反射的对象在使用时 让一切已有的访问权限取消
method.setAccessible(true);
//Object invoke = method.invoke(instance,new LoginResultModel[]{result});
Object invoke = method.invoke(instance,result);
System.out.println(invoke);
Assert.assertEquals(true, invoke);
}

猜你喜欢

转载自www.cnblogs.com/turningli/p/12394192.html
今日推荐