Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso startActivity that depends on Intent

I have the following situation.

My activity has a fragment that depends of 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 can't pass the intent to the activity 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've searched a lot but nothing works. MyObject is always null in the test. I think this should be simple. What am I'm missing?

like image 379
rafael Avatar asked Jul 31 '15 17:07

rafael


People also ask

What is the different between setContentView () function and startActivity () function?

So, with respect to time, setContentView alone is faster, since it doesn't start a new activity. Hence, your app will show the new screen faster... On the other hand, if you call startActivity, this activity is put on the stack, so you can go back by pressing the back button.

What is the name of the method which is use to get intent data in Oncreate ()?

you can retrieve this data using getIntent in the new activity: Intent intent = getIntent(); intent.

What is startActivity ()?

startActivity() simply starts an activity. startActivityForResult() starts an activity and return the generated result in onActivityResult() method of calling activity.


1 Answers

You can define 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);           //...     }  } 
like image 151
Gabriele Mariotti Avatar answered Oct 15 '22 11:10

Gabriele Mariotti