Espresso startActivity, which depends on the intention

I have the following situation.

My activity has a fragment dependent on a Serializable Object. Here is 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 cannot convey the intention of the activity before it was created. I tried with setActivityIntent in several ways, but cannot 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 always null in the test. I think it should be easy. What am I missing?

+4
source share
3 answers

You can determine the intent to be used in this way.

 @RunWith(AndroidJUnit4.class) public class MyActivityTestTest { private MyObject myObj; @Rule // third parameter is set to false which means the activity is not started automatically public ActivityTestRule<MyActivity> mActivityRule = new ActivityTestRule<>(MyActivity.class, false, false); @Test public void testName() { myObj = MyObject.mockObject(); Intent i = new Intent(); i.putExtra("myobj", myObj); mActivityRule.launchActivity(i); //... } } 
+13
source

You can override the ActivityTestRule.getActivityIntent () method and return the desired intent:

 @Rule public ActivityTestRule<MyActivity> mActivityRule = new ActivityTestRule<MyActivity>(MyActivity.class){ @Override protected Intent getActivityIntent() { Intent intent = new Intent(); intent.putExtra("myobj", myObj); return intent; } }; 
+1
source

It doesn't look like you are really starting an activity somewhere.

Try calling getActivity() on the first line of testName ().

This will launch the action you passed to the super constructor.

0
source

All Articles