首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Robolectric:窗口创建失败

Robolectric:窗口创建失败
EN

Stack Overflow用户
提问于 2014-03-17 08:07:23
回答 1查看 1.5K关注 0票数 2

我试图使用robolectric来避免在每次测试运行时启动模拟器。我按照以下(非常好的)教程设置了所有内容:http://www.peterfriese.de/unit-testing-android-apps-with-robolectric-and-eclipse/,不幸的是,我得到了一个错误:

代码语言:javascript
复制
java.lang.RuntimeException: Window creation failed!
at org.robolectric.shadows.ShadowActivity.getWindow(ShadowActivity.java:329)
at org.robolectric.shadows.ShadowActivity.findViewById(ShadowActivity.java:275)
at android.app.Activity.findViewById(Activity.java)
at MainActivityTest.shouldNotBeNull(MainActivityTest.java:32)
    (...)

这是我的密码:

代码语言:javascript
复制
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import static org.fest.assertions.api.ANDROID.assertThat;


import android.widget.Button;
import android.widget.EditText;


import com.abs.databaseprototype.MainActivity;
import com.abs.databaseprototype.R;

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {
private MainActivity activity;

@Before
public void setup() {
  activity = Robolectric.buildActivity(MainActivity.class).get();
}
@Test
public void shouldNotBeNull() {
  assertThat(activity).isNotNull();

  Button btnPatients = (Button) activity.findViewById(R.id.btnPatients);
  assertThat(btnPatients).isNotNull();
//
//    Button btnSpecialties = (Button) activity.findViewById(R.id.btnSpecialites);
//    assertThat(btnSpecialties).isNotNull();
//
//    Button btnTreat = (Button) activity.findViewById(R.id.btnTreat);
//    assertThat(btnTreat).isNotNull();
//    
//    EditText editText = (EditText) activity.findViewById(R.id.edit_query);
//    assertThat(editText).isNotNull();
      }

  @Test
  public void shouldFail() {
      assertTrue(false);
  }
}

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-18 09:04:03

好吧,我找到解决办法了。本教程中有一个错误。您必须首先创建活动,所以编写activity = Robolectric.buildActivity(LoginActivity.class).create().get();,而不是activity = Robolectric.buildActivity(LoginActivity.class).get();,就这样了。我今天学到的东西:总是检查文档。:-)

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22449363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档