简体   繁体   English

使用Robolectric来测试启动活动的代码

[英]Using Robolectric to test code that starts an activity

I have some code I want to test with Robolectric. 我有一些我想用Robolectric测试的代码。 Basically I want to test that a button click launches an activity. 基本上我想测试一个按钮点击启动一个活动。

HomeScreenFragment.java: 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

            }
        }
    }
}

My test looks like this: HomeSreenFragmentTest.java: 我的测试如下所示:HomeSreenFragmentTest.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
    }

The problem I am having is with the test. 我遇到的问题是测试。 The code itself works fine in emulator/on device. 代码本身在仿真器/设备上工作正常。 The problem is that when Robolectric runs the performClick() method, and then gets to onClick() then goes to startActivity(intent) it fails. 问题是,当Robolectric运行performClick()方法,然后转到onClick()然后转到startActivity(intent)它失败。

Stacktrace: 堆栈跟踪:

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)

I know how to start an activity with Robolectric using the Robolectric.buildActivity() method. 我知道如何使用Robolectric.buildActivity()方法开始使用Robolectric进行活动。 But this is for when I need an activity in testing . 但这是因为我需要一个测试活动。 Why is Robolectric failing to run the startActivity() method in code? 为什么Robolectric无法在代码中运行startActivity()方法? Is there a better way to test this? 有没有更好的方法来测试这个?

You should also call .start().resume() on your ActivityController , not only .create() This causes the fragment to be created as well. 您还应该在ActivityController上调用.start().resume() ,而不仅仅是.create()这会导致创建片段。

If you do the above, you won't need to call onCreateView yourself. 如果您执行上述操作,则无需亲自调用onCreateView You can just get the button from the activity using mHomeScreenActivity.findViewById(R.id.sign_in_button); 您可以使用mHomeScreenActivity.findViewById(R.id.sign_in_button);从活动中获取按钮mHomeScreenActivity.findViewById(R.id.sign_in_button);

In general I suspect your code fails because the fragment hasn't fully started yet, because you didn't call start() and resume() 一般来说,我怀疑你的代码失败,因为片段还没有完全启动,因为你没有调用start()resume()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM