Getting Nullpointer exception when UnitTesting Android TabActivity

南笙酒味 提交于 2019-12-10 20:13:50

问题


I am currently unittesting the sample TabLayout from android website. I am getting a NullPointerException when unittesting it like this:

public class MainActivityUnitTest extends ActivityUnitTestCase<MainActivity>{

    public MainActivityUnitTest(){
        super(MainActivity.class);      
    }

    @MediumTest
    public void testStartActivity(){
        setActivity(startActivity(new Intent(Intent.ACTION_MAIN), null, null));
    }
}

This is the exception that I am getting:

java.lang.NullPointerException
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:277)
at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654)
at android.widget.TabHost.setCurrentTab(TabHost.java:326)
at android.widget.TabHost.addTab(TabHost.java:216)
at com.foo.android.MainActivity.onCreate(MainActivity.java:55)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.test.ActivityUnitTestCase.startActivity(ActivityUnitTestCase.java:159)
at com.foo.android.test.MainActivityUnitTest.testStartActivity(MainActivityUnitTest.java:17)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:204)
at android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:194)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)

I tried to search the net with no luck. I am hoping somebody has resolved an issue like this before. What have I done wrong here? Thank you very much in advance for your assistance.


回答1:


I haven't worked with unit testing so I can't say for sure, but it looks like you didn't set the class and context for the startActivity intent.




回答2:


The problem is ActivityUnitTestCase prevents execution of startActivity() to provide isolated environment to test target activity as described in here. When TabWidget invokes addTab() it calls startActivity() to start activity that will fill the tabcontent but startActivity() does nothing, so addTab() fails with NullPointerException.

I can't find any way to avoid this problem. I just switch it to ActivityInstrumentationTestCase2 then test goes fine. But, in this case, I can't use mock context to test other things. Also, it runs activity under actual system (I can see the activity on the screen) very slowly (Just a simple test takes 1 sec on my device). So if you don’t need to mock context, using ActivityInstumentationTestCase2 can be a solution.




回答3:


The problem is ActivityUnitTestCase because it make a test only on "the unit" actionbar isn't in the unit if you want to test it you must extend ActivityInstrumentationTestCase2 and check what say mathume




回答4:


If getActionBar() can return null, check for it:

private void setupActionBar() {
    ActionBar ab = getActionBar();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB && ab != null) {
        ab.setDisplayHomeAsUpEnabled(true);
    }
}

This solved the issue for me.



来源:https://stackoverflow.com/questions/6236977/getting-nullpointer-exception-when-unittesting-android-tabactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!