简体   繁体   中英

startActivity are not working in Android Unit Tests

I've tried everything already. The startActivity are not receiving the intent. Here's the code:

public class ColaboradorTeste extends ActivityUnitTestCase<ColaboradorMainActivity> {

    private UserDAO userDAO;
    private ColaboradorMainActivity activity;

    public ColaboradorTeste() {
        super(ColaboradorMainActivity.class);
    }

    @Override
    protected void setUp() throws Exception{
        super.setUp();

        startActivity(new Intent(getInstrumentation().getTargetContext(), ColaboradorMainActivity.class), null, null);

        activity = (ColaboradorMainActivity)getActivity().getApplicationContext();
        userDAO = new UserDAO(activity);
    }

    public void testBase() throws Exception{
     ...
    }
}

Here's the error:

java.lang.NullPointerException
    at br.fsw.seatafmobile.ColaboradorTeste.setUp(ColaboradorTeste.java:23)

I know the problem is in the startActivity, but I really don't know how to solve it. If anyone could help I'll really appreciate.

I finally found the solution! My test is using a database created on the app. So I had to change the Build Variants from Unit Test to Android Instrumentation Tests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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