getSupportActionBar()使用Robolectric返回null

rev*_*ary 7 android junit4 robolectric android-actionbar android-support-library

当我通过Roboelectric和JUnit中的测试用例调用它时,方法getSupportActionBar()返回null.

这是我的简单测试用例:

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");
}
}
Run Code Online (Sandbox Code Playgroud)

这是我的Activity类:

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

关于hwo的任何好主意要解决这个问题?我是否必须写一些影子活动,或者有人知道如何使用ROBOlectric解决这些操作栏问题吗?

谢谢你的帮助

Mar*_* RS 5

支持ActionBar
我能够通过使用Gingerbread sdk内部版本号为我的测试添加@Config注释来获取Support ActionBar的实例:

@Test @Config(reportSdk = 10)
public void actionbarTest(){
.... Your Test here
}
Run Code Online (Sandbox Code Playgroud)

这里可以看到一个简单的项目设置:simple-robolectric



ActionBarSherlock
您必须将修改后的ActionBarSherlock文件添加到测试包中,并在@Before方法中调用以下方法:

ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockNative.class);
ActionBarSherlock.unregisterImplementation(ActionBarSherlockCompat.class);
Run Code Online (Sandbox Code Playgroud)

完整的说明可以在这里找到:ActionBar和Robolectric一起工作

更新
使用Robolectric 2.2,您只需将配置注释"@Config(reportSdk = 10)"添加到您的测试方法或类中,它也应该可以正常工作.

  • android.view.InflateException:XML文件.\ ..\..\..\..\..\..\android-sdk-x86_64\sdk\extras\android\support\v7\appcompat\res\layout\abc_action_bar_decor_include.xml第#行(对不起,尚未实现):在android.view.LayoutInflater的android.view.LayoutInflater.createView(LayoutInflater.java:613)中输出类android.support.v7.internal.widget.ActionBarContainer时出错android.view.LayoutInflater.parseInclude(LayoutInflater.java:805)中android.view.LayoutInflater.rInflate(LayoutInflater.java:746)的.createViewFromTag(LayoutInflater.java:687) (2认同)