vendredi 31 juillet 2015

Espresso startActivity that depends on Intent

i have the following situation.

My activity have a fragment that depends of a Serializable Object, my onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    MyObject myObj = (MyObj) getIntent().getSerializableExtra("myobj");

    if(myObj != null) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.container, MyFragment.newInstance(myObj));
        transaction.commit();
    }
}

But in my Espresso test i simply cant pass the intent to the actvity before it's created, i tried with setActivityIntent in several ways but cant figure out how to make it work.

Here is my last attempt:

import android.content.Intent;
import android.support.test.InstrumentationRegistry;
import android.support.test.espresso.Espresso;
import android.test.ActivityInstrumentationTestCase2;
import org.junit.Before;

import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

public class MyActivityTest extends

     ActivityInstrumentationTestCase2<MyActivity> {

        private MyActivity activity;
        private MyObject myObj;

        public MyActivityTest() {
            super(MyActivity.class);
        }

        @Before
        protected void setUp() throws Exception {
            super.setUp();
            injectInstrumentation(InstrumentationRegistry.getInstrumentation());
            myObj = MyObject.mockObject();
            Intent i = new Intent();
            i.putExtra("myobj", myObj);
            setActivityIntent(i);

        }

        public void testName(){
            Espresso.onView(withId(R.id.name)).check(matches(withText(myObj.getObjName())));
        }

    }

I searched a lot but nothing works, MyObject is always null in the test, i think this should be simple what im missing?

Aucun commentaire:

Enregistrer un commentaire