【问题标题】:Android unit testing with Robolectrics and ActionBarSherlock使用 Robolectrics 和 ActionBarSherlock 进行 Android 单元测试
【发布时间】:2013-11-19 08:34:39
【问题描述】:

我正在使用 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 个类,并在我的测试类的设置函数中编写了必要的注册和注销代码,我无法为阴影 mActivity 设置 contentview: shadowOf(mActivity).setContentView(contentView);

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

你有什么想法在测试时跳过这个 ActionBarSherlock 错误吗? 谢谢。

【问题讨论】:

  • 您在测试中使用了reportSdk = 10。您是否为 API 10 设置了正确的主题?也许res/values-v11 包含一个夏洛克主题,但res/values 没有?
  • @Nachi 我不熟悉这些样式文件。如何检查 API 10 的主题设置是否正确?我应该看看 res/values/style.xml 吗?顺便说一句, minSdk : 8 , targetSdk 。 17 我的项目
  • 发布所有 style.xml 文件。这些应该在res/values 和任何res/values-vXX 文件夹下。
  • 你好,我在 github 上创建了这个要点。 Robolectric 2.2 不需要它。您的问题与您扩展的主题有关。当您使用 Actionbarsherlock 时,您是否尝试过在姜饼设备上运行您的应用程序?另请参阅此处:github.com/marsucsb/simple-robolectric/blob/master/res/values/… 我如何从 Theme.AppCompat 扩展?你必须做类似的事情。
  • 我可以通过将gist.github.com/marsucsb/6059760 修改为使用mActivity.getWindow().setContentView(view); 来解决类似的问题,请参阅gist.github.com/justinmuller/8025465 的要点

标签: android unit-testing junit actionbarsherlock robolectric


【解决方案1】:

正如我在 different answer 中提到的,here is a Gist 采用 @Xian 的 Gistmarsucsb 的建议,并对其进行修改以与 Robolectric 2.2+ 一起使用

要回答这个特定问题,shadowOf(mActivity) 在 Robolectric 2.2+ 中不再可用,因此应使用 mActivity.getWindow().setContentView(view) 而不是 shadowOf(mActivity).setContentView(view)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2016-06-15
    相关资源
    最近更新 更多