getSupportActionBar()与Robolectric返回null [英] getSupportActionBar() returns null with Robolectric

查看:291
本文介绍了getSupportActionBar()与Robolectric返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过总部设在Roboelectric和JUnit测试案例调用它的方法getSupportActionBar()返回null。

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");
}
}

这是我的Activity类:

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);
    }
}
}

什么好想法藿解决这一问题? 我必须写一些阴影活动或有没有人知道如何使用这些操作栏问题RObolectric ??

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??

感谢您的帮助

推荐答案

支持动作条
我能找回支持动作条的实例,通过添加@Config注释我的测试与姜饼SDK版本号:

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
}

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

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



ActionBarSherlock
你要修改的ActionBarSherlock文件添加到您的测试包,并调用以下方法在你的@Before方法:



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);

完整的指令集,可以在这里找到:动作条和Robolectric合作

更新
随着Robolectric 2.2,你只需要在配置注解@Config(reportSdk = 10)添加到您的测试方法或类,它应该正常工作。

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.

这篇关于getSupportActionBar()与Robolectric返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆