0

This is my code for the instrumented test class

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();
    } 

I am getting a particular error for this whenever i try to run this code. Is there any particular way to work with fragments UI for doing instrumented testing in android.It generally shows no views found or null pointer exception or error related to onSaveInstance method

This is the actula error i am getting

Kishan Viramgama
  • 893
  • 1
  • 11
  • 23

0 Answers0