gpt4 book ai didi

android - Robolectric:窗口创建失败

转载 作者:太空狗 更新时间:2023-10-29 15:53:25 24 4
gpt4 key购买 nike

我正在尝试使用 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);
}
}

有什么想法吗?

最佳答案

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

关于android - Robolectric:窗口创建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22449363/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com