这是我用于测试类的代码
public class LoginActivityTest2 {
String name = "suryansh mishra";
@Rule
public ActivityTestRule<SecondActivity> mActivityTestRule = new ActivityTestRule<>(SecondActivity.class, false);
private SecondActivity mActivity = null;
@Before
public void setup() {
mActivity = mActivityTestRule.getActivity();
}
/* @Test
public void checkUI(){
// mActivityTestRule.getActivity().getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, mFragment, null).commit();
// TextView mTextView = mActivityTestRule.getActivity().findViewById(R.id.textview_one);
// assertEquals(InstrumentationRegistry.getTargetContext().getString(R.string.hello_blank_fragment),mTextView.getText().toString().trim());
onView(withId(R.id.textview_one))
.perform(typeText(name), closeSoftKeyboard());
}
*/
@Test
public void checkUIOne() {
/*ConstraintLayout rlContainer = (ConstraintLayout) mActivity.findViewById(R.id.fragment_container);
Fragment fragment = new BlankFragment();
mActivityTestRule.getActivity().getSupportFragmentManager().beginTransaction().add(rlContainer.getId(), fragment).commitAllowingStateLoss();
// View view = fragment.getView().findViewById(R.id.textview_two);*/
// assertNotNull(view);
TextView mTextView = mActivityTestRule.getActivity().findViewById(R.id.textview_one);
final Button button = mActivityTestRule.getActivity().findViewById(R.id.click_perform);
assertNotNull(mTextView);
mTextView.setText("Hello blank fragment");
assertEquals(InstrumentationRegistry.getTargetContext().getString(R.string.hello_blank_fragment), mTextView.getText().toString().trim());
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openFragment();
/*TextView anotherText = mActivityTestRule.getActivity().findViewById(R.id.another_text);
assertEquals("message to another fragment",anotherText.getText().toString().trim());*/
}
});
button.post(new Runnable() {
@Override
public void run() {
button.performClick();
}
});
/*try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}*/
// TextView anotherText = mActivityTestRule.getActivity().findViewById(R.id.another_text);
}
private void openFragment() {
Bundle bundle = new Bundle();
bundle.putString("edttext", "This is a text");
Fragment fragment = new BlankFragmentTwo();
fragment.setArguments(bundle);
FragmentManager fm = mActivityTestRule.getActivity().getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
}
每当我尝试运行此代码时,都会收到一个特定的错误消息。是否有任何特殊的方式可以使用片段UI在android中进行仪器化测试。它通常不显示任何视图,空指针异常或与onSaveInstance方法相关的错误