开发者

Getting Nullpointer exception when UnitTesting Android TabActivity

开发者 https://www.devze.com 2023-03-11 12:58 出处:网络
I am currently unittesting the sample TabLayout from android website. I am getting a NullPointerException when unittesting it like this:

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 开发者_JAVA百科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.


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.


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号