- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Android 库修改了 DecimalFormat class加上一些不错的补充。当然,我需要添加其中一项(有效数字表示法:@
)。
当我使用 Robolectric 进行测试时,似乎标准 Java DecimalFormat使用了类,这使我的测试失败。
有没有办法告诉 Robolectric 使用 Android 版本?或者我应该在我的项目中包含 DecimalFormat
(从 AOSP 复制/粘贴类?)?
这是一个精简的例子:
如果我在具有以下设备的设备上运行该应用程序:
import java.text.DecimalFormat;
import java.util.Locale;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
DecimalFormat decimalFormat = (DecimalFormat)java.text.NumberFormat.getInstance(Locale.US);
decimalFormat.applyPattern("@@@");
Log.d('SO', decimalFormat.format(0.0567467));
}
}
它显示:
W/App: 0.0567
在单元测试中:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import java.text.DecimalFormat;
import java.util.Locale;
import static junit.framework.TestCase.assertEquals;
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class NumberFormatTest {
@Test
public void testSignificantDigit() {
DecimalFormat decimalFormat = (DecimalFormat)java.text.NumberFormat.getInstance(Locale.US);
decimalFormat.applyPattern("@@@");
assertEquals("0.0567", decimalFormat.format(0.0567467));
}
}
运行测试将输出:
junit.framework.ComparisonFailure:
Expected :0.0567
Actual :@@@0
最佳答案
作为official android documentation指出,在 Android 运行时 (ART) 中运行 Java 代码时提供的实现不同于常规 JVM 中的实现。
RoboElectric 不在普通 Android 手机或模拟器上运行测试,而是在 JVM 上运行。在您的主机系统上运行的这个 JVM 没有 DecimalFormat 的这个修改后的运行时实现版本。因此,据我所知,在 JVM 中运行测试代码时,Android 提供的功能不可用。为了能够使用它,请在 Android 手机上运行测试,但是无论如何您都可以摆脱 RoboElectric 进行该测试,但使用 offered testing tools .
另一个逻辑问题:为什么要测试Android类?我猜谷歌已经为那里的变化编写了测试并跟踪它,所以you should no test functionality offered to you by the system you're using .
关于java - 使用 Robolectric 测试 Android DecimalFormat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864889/
有没有办法在使用 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 模块并
我是一名优秀的程序员,十分优秀!