我有一些代码想要用Robolectric进行测试。基本上,我想测试当点击一个按钮时是否会启动一个活动。
HomeScreenFragment.java:
public class HomeScreenFragment extends Fragment {
private Button mSignInButton;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState); // call to super class
}
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup parent,
Bundle savedInstanceState){
// inflate view
View view = inflater.inflate(R.layout.fragment_home_screen, parent, false);
// handle sign in button
mSignInButton = (Button)view.findViewById(R.id.sign_in_button);
mSignInButton.setOnClickListener(new View.OnClickListener() {
// anonymous inner class
@Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), SignInActivity.class);// start sign in activity with intent
startActivity(intent); // <<== ERROR HERE WHEN RUNNING TEST
}
}
}
}
我的测试看起来像这样: HomeScreenFragmentTest.java:
@RunWith(RobolectricTestRunner.class)
public class HomeScreenFragmentTest {
private Activity mHomeScreenActivity;
private Fragment mTestFragment;
private Button mSignInButton;
@Before
public void setup() throws Exception{
mHomeScreenActivity = Robolectric.buildActivity(HomeScreenActivity.class).create().get(); // start HomeScreenActivity, call through to onCreate()
mTestFragment = mHomeScreenActivity.getFragmentManager().findFragmentById(R.id.home_screen_fragment_container);// get HomeScreenFragment
// run onCreateView
View testView = mTestFragment.onCreateView(LayoutInflater.from(mHomeScreenActivity),
(ViewGroup) mHomeScreenActivity.findViewById(R.id.home_screen_fragment_container),
null);
// get button view
mSignInButton = (Button)testView.findViewById(R.id.sign_in_button);
}
// clicking sign in button should launch SignInActivity
@Test
public void testSignInButton2() throws Exception{
mSignInButton.performClick(); <<=== ERROR STARTS HERE
ShadowActivity shadowActivity = Robolectric.shadowOf(mHomeScreenActivity); // create shadow activity
Intent startedIntent = shadowActivity.getNextStartedActivity(); // get intent of next activity on stack
ShadowIntent shadowIntent = Robolectric.shadowOf(startedIntent); // create shadow intent which starts next activity
assertEquals(SignInActivity.class.getName(), shadowIntent.getComponent().getClassName()); // compare shadow intent w/ desired next activity
}
我遇到的问题与测试有关。代码在模拟器/设备上本身运行良好。但问题在于当Robolectric运行performClick()
方法,并且到达onClick()
然后跳转到startActivity(intent)
时,它会失败。
堆栈跟踪:
java.lang.NullPointerException: null
at android.app.Activity.startActivityFromFragment(Activity.java:3850)
at android.app.Activity.startActivityFromFragment(Activity.java:3825)
at android.app.Fragment.startActivity(Fragment.java:996)
at android.app.Fragment.startActivity(Fragment.java:975)
at com.********.android.***project*****.controller.HomeScreenFragment$1.onClick(HomeScreenFragment.java:42)
at android.view.View.performClick(View.java:4084)
at com.*********.android.***project***.HomeScreenFragmentTest.testSignInButton2(HomeScreenFragmentTest.java:83)
我知道如何使用Robolectric.buildActivity()
方法启动Robolectric中的活动,但这只适用于需要进行测试的活动。为什么在代码中Robolectric无法运行startActivity()
方法?是否有更好的测试方法?
startActivity
方法由ShadowContextWrapper
处理,但是Fragment
不像Activity
和Service
对象一样是一个Context
,因此尝试运行真正的Android代码时会出错。我将其作为问题#767提交了。 - Corey DstartActivity()
方法以获得灵感。 - Corey D