Espresso startActivity that depends on Intent
18,637
Solution 1
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);
//...
}
}
Solution 2
You can override the ActivityTestRule.getActivityIntent() method and return a required 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;
}
};
Related videos on Youtube
![rafael](https://i.stack.imgur.com/n1cv0.jpg?s=256&g=1)
Author by
rafael
Updated on June 06, 2022Comments
-
rafael about 2 years
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?-
yogurtearl almost 9 yearsWhat error message are you getting?
-
yogurtearl almost 9 yearsyou don't need @Before, because this is a JUnit 3 test not a JUnit 4 test.
-
rafael almost 9 yearsSolved, the problem was with my mock... Sorry to bother.
-
Carlos Daniel about 5 yearsSolution in Kotlin taken from here
-
-
rafael almost 9 yearsI tried to call
getActivity()
on the first line of test method and in the last line ofsetUp()
but didn't work =/ -
rafael almost 9 yearsSolved, the problem was with my mock... Sorry to bother.
-
IgorGanapolsky almost 4 yearsWhere are you passing
myObj
into this Rule?