如何运行在一个活动片段的测试 [英] How to run tests on an activities fragment
问题描述
我刚刚起步的王氏的JUnit
和我遇到的第一个问题,我应该怎么测试片段?
I am just starting out wih junit
and the first issue I'm running into is, how should I test fragments?
被测试的活动有1片段是主要的布局。
The activity being tested has 1 fragment which is the main layout.
@Override
protected void setUp() throws Exception {
super.setUp();
Intent intent = new Intent(getInstrumentation().getTargetContext(),
ActivityWelcome.class);
startActivity(intent, null, null);
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
mFragmentWelcome = (FragmentWelcome) fragmentManager.findFragmentByTag(FragmentWelcome.TAG);
if (mFragmentWelcome == null) {
mFragmentWelcome= FragmentWelcome.newInstance();
fragmentManager
.beginTransaction()
.replace(android.R.id.content, mFragmentWelcome, FragmentWelcome.TAG)
.commit();
}
}
我接下来要测试的布局:
I then go on to test the layout:
@SmallTest
public void testLayout() {
ImageButton buttonImage = (ImageButton) getActivity().findViewById(my.package.R.id.button_invites);
assertNotNull(buttonImage);
assertEquals("ImageButton Invite has an invalid drawable"
, getActivity().getResources().getDrawable(my.package.R.drawable.light_social_invite)
, buttonImage);
}
错误:
java.lang.NullPointerException
at my.package.test.ActivityWelcomeUnitTest.testLayout(ActivityWelcomeUnitTest.java:55)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:199)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:545)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1551)
失败的:
ImageButton buttonImage = (ImageButton) mFragmentWelcome.getView().findViewById(my.package.R.id.button_invites);
我不知道为什么片段是空的,我显然不正确地执行此操作,可能会有人请赐教?
I'm not sure why the fragment is null, I am obviously performing this operation incorrectly, could someone please enlighten me?
推荐答案
其实你必须等待UI线程组成的片段的主视图。
Actually you have to wait for the UI thread composes the main view with the fragments.
我有同样的错误,所以我看着Robotium的来源,看看他们是如何处理的。响应是他们使用的等待所需时间的一些方法。
I had the same errors and so I looked at the sources of Robotium to see how they handle that. The response is they use some methods that wait the required time.
有关片段,你可以执行你的说法之前,使用这样的事情:
For fragment you can use something like that before performing your assertions :
protected Fragment waitForFragment(String tag, int timeout) {
long endTime = SystemClock.uptimeMillis() + timeout;
while (SystemClock.uptimeMillis() <= endTime) {
Fragment fragment = getActivity().getSupportFragmentManager().findFragmentByTag(tag);
if (fragment != null) {
return fragment;
}
}
return null;
}
我用5000毫秒的超时,它似乎运作良好。
I use a timeout of 5000ms, it seems to work well.
因此,在您code就应该更换
So in your code you should replace
mFragmentWelcome = (FragmentWelcome) fragmentManager.findFragmentByTag(FragmentWelcome.TAG);
按
mFragmentWelcome = (FragmentWelcome) waitForFragment(FragmentWelcome.TAG, 5000);
编辑:此响应假定您的JUnit扩展 ActivityInstrumentationTestCase2&LT; T&GT;
这篇关于如何运行在一个活动片段的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!