如何测试由片段使用的 Activity 实现的接口?
How can I test an interface implemented by an Activity which is used by a fragment?
我有一个 Activity
,其中包含两个片段。一个片段有一个 EditText
和一个 Button
;单击按钮时,将使用编辑框中的文本调用回调:
// Not showing implementation of onCreateView()
class HelloEditFragment extends Fragment implements View.OnClickListener {
interface OnListener {
void onValue(String value);
}
private OnListener mListener;
public void onAttach(Context context) {
mListener = (OnListener) context;
}
public void onClick(View v) {
mListener.onValue(mEditText.getText().toString();
}
}
我的 Activity 实现 OnListener:
class HelloActivity extends Activity implements HelloEditFragment.OnListener {
public void onValue(String value) {
// ...
}
}
到目前为止我的测试是这样的:
@RunWith (AndroidJUnit4.class)
public class HelloEditFragmentTest {
@Rule
public ActivityTestRule<HelloActivity> mActivityRule = new ActivityTestRule<> (HelloActivity.class);
@Test
public void testActivity () {
onView (withId (R.id.edit_text_hello_edit)).perform (typeText ("hello"));
onView (withId (R.id.button_hello_edit)).perform (click ());
// Now what?
}
}
如何检查 onValue()
回调在片段中的按钮被点击时被调用?谢谢,
没有简单的方法可以做到这一点。我建议您放弃由 Activity
实现 interface
并改用字段监听器。
例如像这样:
class HelloActivity extends Activity {
HelloEditFragment.OnListener listener = new HelloEditFragment.OnListener() {
@Override
public void onValue(String value) {
// ...
}
};
public HelloEditFragment.OnListener getListener() {
return listener;
}
//... everything else
}
class HelloEditFragment extends Fragment implements View.OnClickListener {
public void onAttach(Context context) {
if (context instanceof HelloActivity) {
mListener = ((HelloActivity) context).getListener();
}
}
//... everything else (including onDetach with clearing the listener)
}
然后你就可以轻松地模拟你的现场监听器了:
@RunWith (AndroidJUnit4.class)
public class HelloEditFragmentTest {
@Rule
public ActivityTestRule<HelloActivity> mActivityRule = new ActivityTestRule<> (HelloActivity.class);
@Test
public void testActivity () {
HelloEditFragment.OnListener listener = Mockito.mock(HelloEditFragment.OnListener.class);
mActivityRule.getActivity().listener = listener;
onView (withId (R.id.edit_text_hello_edit)).perform (typeText ("hello"));
onView (withId (R.id.button_hello_edit)).perform (click ());
Mockito.verify(listener, Mockito.times(1)).onValue("hello");
Mockito.verifyNoMoreInteractions(listener);
}
}
我有一个 Activity
,其中包含两个片段。一个片段有一个 EditText
和一个 Button
;单击按钮时,将使用编辑框中的文本调用回调:
// Not showing implementation of onCreateView()
class HelloEditFragment extends Fragment implements View.OnClickListener {
interface OnListener {
void onValue(String value);
}
private OnListener mListener;
public void onAttach(Context context) {
mListener = (OnListener) context;
}
public void onClick(View v) {
mListener.onValue(mEditText.getText().toString();
}
}
我的 Activity 实现 OnListener:
class HelloActivity extends Activity implements HelloEditFragment.OnListener {
public void onValue(String value) {
// ...
}
}
到目前为止我的测试是这样的:
@RunWith (AndroidJUnit4.class)
public class HelloEditFragmentTest {
@Rule
public ActivityTestRule<HelloActivity> mActivityRule = new ActivityTestRule<> (HelloActivity.class);
@Test
public void testActivity () {
onView (withId (R.id.edit_text_hello_edit)).perform (typeText ("hello"));
onView (withId (R.id.button_hello_edit)).perform (click ());
// Now what?
}
}
如何检查 onValue()
回调在片段中的按钮被点击时被调用?谢谢,
没有简单的方法可以做到这一点。我建议您放弃由 Activity
实现 interface
并改用字段监听器。
例如像这样:
class HelloActivity extends Activity {
HelloEditFragment.OnListener listener = new HelloEditFragment.OnListener() {
@Override
public void onValue(String value) {
// ...
}
};
public HelloEditFragment.OnListener getListener() {
return listener;
}
//... everything else
}
class HelloEditFragment extends Fragment implements View.OnClickListener {
public void onAttach(Context context) {
if (context instanceof HelloActivity) {
mListener = ((HelloActivity) context).getListener();
}
}
//... everything else (including onDetach with clearing the listener)
}
然后你就可以轻松地模拟你的现场监听器了:
@RunWith (AndroidJUnit4.class)
public class HelloEditFragmentTest {
@Rule
public ActivityTestRule<HelloActivity> mActivityRule = new ActivityTestRule<> (HelloActivity.class);
@Test
public void testActivity () {
HelloEditFragment.OnListener listener = Mockito.mock(HelloEditFragment.OnListener.class);
mActivityRule.getActivity().listener = listener;
onView (withId (R.id.edit_text_hello_edit)).perform (typeText ("hello"));
onView (withId (R.id.button_hello_edit)).perform (click ());
Mockito.verify(listener, Mockito.times(1)).onValue("hello");
Mockito.verifyNoMoreInteractions(listener);
}
}