ES preSSO startActivity取决于意图 [英] Espresso startActivity that depends on Intent
本文介绍了ES preSSO startActivity取决于意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下的情况。
我的活动都有所依赖的序列化对象的片段。这是我的onCreate:
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();
}
}
但在我的长者preSSO测试,我根本无法意图的创建之前将它传递给活动。我setActivityIntent试过多种方法,但无法弄清楚如何使它发挥作用。
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.
这是我最后一次尝试:
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())));
}
}
我已经搜查了很多,但没有任何工程。 为MyObject
总是在测试空。我想这应该很简单。我是什么我失踪?
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?
推荐答案
您可以定义意图以这种方式使用
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(intent);
//...
}
}
这篇关于ES preSSO startActivity取决于意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文