简体   繁体   English

startActivity在Android单元测试中不起作用

[英]startActivity are not working in Android Unit Tests

I've tried everything already. 我已经尝试了一切。 The startActivity are not receiving the intent. startActivity没有收到该意图。 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. 我知道问题出在startActivity中,但我真的不知道如何解决。 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. 因此,我不得不将Build Variants从Unit Test更改为Android Instrumentation Tests。

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

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