我有这样的方法
public void updateListViewVisibility(List<User> userList) {
userViewVisibility.set(View.GONE);
handler.postDelayed(runnableCode, 10000000);
}
/**
* A runnable code, the purpose of this is to display a message if no user found in range.
*/
private Runnable runnableCode = new Runnable() {
@Override
public void run() {
changeState();
}
};
如果方法updateListViewVisibility
中没有处理程序,我可以对此进行单元测试。
@Test
public void test_updateupdateListViewVisibility() {
List<User> userList = new ArrayList<User>();
userList.clear();
SUT.updateListViewVisibility(userList);
assertEquals("Variable userViewVisibility should be false",
SUT.userViewVisibility.get(), false);
}
但是,如果我保留处理程序
我收到以下错误。
java.lang.RuntimeException: Method postDelayed in android.os.Handler not mocked. See http://g.co/androidstudio/not-mocked for details.
如何正确测试该方法?
答案 0 :(得分:0)
这很简单,运行您的Test类MockitoJunitRunner
例如:waitpid
模拟处理程序
@RunWith(MockitoJUnitRunner.class)
public class WhateverClassTest { }
///将这些依赖项添加到gradle中:
@Mock
Handler handler