getSupportActionBar() returns null with Robolectric

不问归期 提交于 2019-12-21 12:09:20

问题


The method getSupportActionBar() returns null when i invoke it through a Test Case based in Roboelectric and JUnit.

This is my simple test case:

package com.mobile.test;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import android.app.Activity;
import android.content.Intent;
import com.mobile.android.core.R;
import com.mobile.android.core.activity.MainActivity;
import com.mobile.android.core.activity.TestActivity;

@RunWith(RobolectricTestRunner.class)
public class NavigationDrawerTest {
private Activity activity;

@Test
public void testNavigationDrawer() {
    activity = Robolectric.buildActivity(MainActivity.class).create().get();
    String hello = activity.getResources().getString(R.string.drawer_open);
    System.out.println(hello);
    assertEquals(hello, "Menu");
}
}

And this is my Activity class:

public class MainActivity extends ActionBarActivity {
// Drawer related
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
String[] mDrawerOptions;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // enable ActionBar app icon to behave as action to toggle nav-drawer
    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
    }
}
}

Any bright ideas on hwo to fix this?? DO i have to write some shadow activity or does anyone knows how to work with these action bar problems with RObolectric??

Thanks for any help


回答1:


Support ActionBar
I was able to get back an instance of the Support ActionBar by adding a @Config annotation to my test with the Gingerbread sdk build number:

@Test @Config(reportSdk = 10)
public void actionbarTest(){
.... Your Test here
}

A simple project setup can be seen here: simple-robolectric



ActionBarSherlock
You have to add modified ActionBarSherlock files to your test package and call the following methods in your @Before method:

ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockNative.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockCompat.class);

The full set of instructions can be found here: ActionBar and Robolectric working together

Update
With Robolectric 2.2 you only have to add the config annotation "@Config(reportSdk = 10)" to your test methods or class and it should work as well.



来源:https://stackoverflow.com/questions/17993239/getsupportactionbar-returns-null-with-robolectric

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