我正在尝试测试RecyclerView项目中的视图。单独运行测试时,所有这些都可以正常工作,但是尝试测试整个类时,我会得到
java.lang.Exception:没有可运行的方法
我创建了一个帮助器类以单击指定的视图:(在androidTest目录中)
public class RecyclerViewTools {
/**
* This method clicks a specified view in RecyclerView's item
* @param id position of item to perform click
*/
public ViewAction clickChildWithId(final int id){
return new ViewAction() {
@Override
public Matcher<View> getConstraints() {
return null;
}
@Override
public String getDescription() {
return null;
}
@Override
public void perform(UiController uiController, View view) {
View v = view.findViewById(id);
v.performClick();
}
};
}}
和我在另一堂课中的测试
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
private RecyclerViewTools recyclerViewTools;
@Rule
public ActivityTestRule rule = new ActivityTestRule<>(MainActivity.class);
@Before
public void setUp(){
recyclerViewTools = new RecyclerViewTools();
}
@After
public void tearDown(){
recyclerViewTools = null;
}
/*
* This tests check if all buttons in recyclerview are working properly
* Shorten variables:
* ToggleButton -tbn
*/
@Test
public void TBNs_isChecked(){
onView(withId(R.id.recylerview_alarms))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.monday_togglebutton)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.tuesday_togglebutton)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.wednesday_togglebutton)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.thursday_togglebutton)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.friday_togglebutton)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.saturday_togglebutton)))
.perform(RecyclerViewActions.actionOnItemAtPosition(0,
recyclerViewTools.clickChildWithId(R.id.sunday_togglebutton)));
}
// Check if switch is being checked
@Test
public void switch_isChecked(){
onView(withId(R.id.recylerview_alarms))
.perform(RecyclerViewActions.actionOnItemAtPosition
(0, recyclerViewTools.clickChildWithId(R.id.enable_switch)));
}}
如我所读,问题的根源可能是使用静态方法(当时是clickChildWithId(id)
,我决定每次运行测试时都要初始化该类。唯一的问题,我可以看到并拥有不知道如何避免它,是返回ViewAction
的辅助方法。
我该怎么解决我的问题?