- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的小部件,它由一个图像和一个按钮组成,该按钮应该启动一个 Activity 。我正在尝试编写一个 Robolectric 测试来测试单击按钮时是否启动了 Activity
我有两个问题,首先我在尝试单击按钮时收到 NPE:
java.lang.NullPointerException: can't get a shadow for null
at org.robolectric.bytecode.ShadowWrangler.shadowOf(ShadowWrangler.java:415)
at org.robolectric.Robolectric.shadowOf_(Robolectric.java:1020)
at org.robolectric.Robolectric.shadowOf(Robolectric.java:671)
at org.robolectric.shadows.ShadowIntent.fillIn(ShadowIntent.java:454)
at android.content.Intent.fillIn(Intent.java)
at org.robolectric.shadows.ShadowPendingIntent.send(ShadowPendingIntent.java:48)
at android.app.PendingIntent.send(PendingIntent.java)
at org.robolectric.shadows.ShadowRemoteViews$2$1.onClick(ShadowRemoteViews.java:61)
at android.view.View.performClick(View.java:4084)
此外,我不确定如何获取对通过单击按钮启动的 Activity 的引用。
测试代码:
@Test
public void buttonShouldLaunchActivity() throws Exception {
int widgetId = shadowAppWidgetManager.createWidget(HelloWidgetProvider.class, R.layout.hellowidget_layout);
View helloWidgetView = shadowAppWidgetManager.getViewFor(widgetId);
Button quickButton = (Button) helloWidgetView.findViewById(R.id.quick_add_button);
quickButton.performClick();
// Not sure how to get a handle of the activity started from a widget, this is what I have for an activity launched from another activity.
Intent intent = Robolectric.shadowOf(activity).peekNextStartedActivity();
assertEquals(QuickAddActivity.class.getCanonicalName(), intent.getComponent().getClassName());
}
如有任何想法,我们将不胜感激,实际的小部件正在运行( Activity 已启动),但我只想对其进行测试。
最佳答案
peekNextStartedActivity()
实际上是 ShadowApplication 上的一个方法。 ShadowContextWrapper 上的方法(这是 ShadowActivity 隐式使用的方法)实际上只是一个调用影子应用程序的包装器。
所以你应该能够做这样的事情,以获得你需要的东西:
Robolectric.getShadowApplication().peekNextStartedActivity()
关于java - Robolectric 测试从小部件启动的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963852/
有没有办法在使用 Robolectric 运行测试时修复系统时钟?我有一些代码取决于星期几。在 rspec 中,有 TimeCop gem 允许您为系统修复时间。 Robolectric 有等效项吗?
实际上,我需要获取 API 调用的响应,为此我需要 Context。 最佳答案 更新。 仅用于版本 1.x 和 2.x: Robolectric.application; 对于 3.x 版本: Run
我在使用 roboelectric 2.2 时遇到以下错误 java.lang.IncompatibleClassChangeError: class org.objectweb.asm.tree.C
我正在使用 Robolectric用于我的单元测试并更新了我的Robolectric从 1.2 到 2.2 的 jar 并试图弄清楚如何在这个新版本中绑定(bind)影子类。这是我之前在做的: Rob
将 LeakCanary (1.3) 添加到我的应用程序中: @Override public void onCreate() { super.onCreate(); Fabric.w
我按照官方教程将 robolectric 添加到我现有的 Android 应用程序中。但是当我尝试运行一个简单的测试时,我收到了一个错误: java.lang.RuntimeException: No
我正在为一个使用多 dex 的项目配置 Robolectric。我正在使用 Robolectric 3.0 RC3,它应该支持 Multidex 而不会抛出“Multi dex 安装失败”异常。 te
我是 Robolectric 的新手,请帮助我理解它,这些有什么区别 loginActivity = new LoginActivity();loginActivity = Robolectric.s
在 Android Studio 中同步我的 Gradle 项目时,出现以下错误: Error:Could not find any version that matches org.robolect
我正在尝试使用 Robolectric 设置测试以单击 this repository 中的菜单按钮.基本的 Robolectric 测试将运行,但我无法使用资源运行任何特定于项目的测试,因为它说它找
我在网上搜索了如何使用 android studio 配置 robolectric。 网上有很多资料,但没有一个适合我。每个人都有自己的使用android studio配置Robolectric的步骤
我正在使用 robolectric 来测试一项使用 Google Analytics 的 Activity 。不幸的是,每当我尝试启动 Activity 时,我都会遇到以下异常 android.dat
我按照列出的方式运行迁移 here 运行测试后,我收到带有内部堆栈跟踪的错误 java.lang.RuntimeException: java.lang.ClassNotFoundException:
我在运行测试时出现间歇性故障。 java.lang.NullPointerException at org.robolectric.res.ResBundle$Value.compareTo(
我曾经使用 Android 单元测试运行我的单元测试,但后来切换到使用 Robolectric 的 Java 单元测试。我注意到使用 SaxParser DefaultHandler 的不同之处。基本
我在测试 getView 时遇到错误。测试代码在测试用例期间调用 View.setImageResource(0) 以在异常发生时设置空图像。 我用 Eclipse 运行了测试 robolectric
使用 Robolectric.buildActivity().setup() 的 JUnit 测试在 AndroidX 和 Robolectric 4.3 下不一致地失败。通常在测试套件中,像这样的前
使用.withIntent()创建事件在Robolectric 2中不起作用吗?我正在做以下 activity = Robolectric.buildActivity(MyActivity.c
我知道 Robolectric 官方网站上说这个框架用于单元测试,但在所有示例 robolectric 上都用于测试 View ...例如:Robolectric 可用于检查 textview X 的
是否可以在自己的模块中设置 robolectric 并将其作为依赖项添加到我的项目中?我可以将它添加到我的项目模块中,但如果它在它自己的模块中,我更喜欢它。我创建了一个 javaLibrary 模块并
我是一名优秀的程序员,十分优秀!