使用Robolectrics和ActionBarSherlock进行Android单元测试

时间:2013-11-19 08:34:39

标签: android unit-testing junit actionbarsherlock robolectric

我使用Robolectric来测试我的LoginActivity类。我在getSupportActionBar()行上使用ActionBarSherlock和Robolectric测试失败。这是我的代码和跟踪。

LoginActivity.java

import org.json.JSONException;
import org.json.JSONObject;
import com.actionbarsherlock.app.SherlockActivity;

public class LoginActivity extends SherlockActivity {
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        ...
...
..
 }

LoginActivityTest.java

import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
..
.
@RunWith(RobolectricTestRunner.class)
public class LoginActivityTest {

     private LoginActivity activity;
     @Before
    public void setUp() throws Exception
    {
        }

   @Test @Config(reportSdk = 10)
    public void shouldActivityCreated() throws Exception {
           activity = Robolectric.buildActivity(LoginActivity.class).create().get();
           assertNotNull(activity);
    }
}

当我尝试JUnit测试时,我的跟踪失败了:

java.lang.IllegalStateException: You must use Theme.Sherlock, Theme.Sherlock.Light, Theme.Sherlock.Light.DarkActionBar, or a derivative.
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.generateLayout(ActionBarSherlockCompat.java:1003)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.installDecor(ActionBarSherlockCompat.java:915)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.initActionBar(ActionBarSherlockCompat.java:138)
    at com.actionbarsherlock.internal.ActionBarSherlockCompat.getActionBar(ActionBarSherlockCompat.java:128)
    at com.actionbarsherlock.app.SherlockActivity.getSupportActionBar(SherlockActivity.java:37)
    at auth.LoginActivity.onCreate(LoginActivity.java:92)
    at android.app.Activity.performCreate(Activity.java:5008)
    at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:116)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:123)
    at LoginActivityTest.shouldActivityCreated(LoginActivityTest.java:85)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

经过研究,我发现了这一点:https://gist.github.com/marsucsb/6059760 我创建了这3个类,并编写了必要的注册和我的测试类中的设置功能中的取消注册代码,我无法设置内容视图的阴影动态: shadowOf(mActivity).setContentView(内容查看);

我正在使用Eclipse和Robolectric 2.2-jar-with-dependencies

您是否有任何想法在测试时跳过此ActionBarSherlock错误? 感谢。

1 个答案:

答案 0 :(得分:1)

正如我在different answer here is a Gist中提到的那样,@ Gist提出了来自marsucsb的建议,并将其修改为与Robolectric 2.2 +一起使用

要回答此特定问题,Robolectric 2.2+中不再提供shadowOf(mActivity),因此应使用mActivity.getWindow().setContentView(view)代替shadowOf(mActivity).setContentView(view)