【问题标题】:Robolectric: Window creation failedRobolectric:窗口创建失败
【发布时间】:2014-03-17 08:07:23
【问题描述】:

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

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)
    (...)

这是我的代码:

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

有什么想法吗?

【问题讨论】:

  • 你解决了吗?如果是,请选择一个答案。

标签: android eclipse testing robolectric


【解决方案1】:

好的,我找到了解决方案。 教程有错误。你必须先创建活动,所以写 活动 = Robolectric.buildActivity(LoginActivity.class).create().get(); 代替 活动 = Robolectric.buildActivity(LoginActivity.class).get(); 你去吧。 我今天学到的东西:总是检查文档。 :-)

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 2014-06-03
    • 2012-07-22
    • 2021-03-17
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多