简体   繁体   English

使用Robolectric测试Android DecimalFormat

[英]Test Android DecimalFormat with Robolectric

The Android library modified the DecimalFormat class with some nice additions. Android库对DecimalFormat类进行了一些改进。 Of course, I need one of those additions (the signifant digit notation: @ ). 当然,我需要这些加法之一(有效数字表示法@ )。

When I'm testing with Robolectric, it seems that the standard Java DecimalFormat class is used, which makes my tests fail. 当我用Robolectric测试时,似乎使用了标准的Java DecimalFormat类,这使我的测试失败了。

Is there a way to tell Robolectric to use the Android version? 有没有办法告诉Robolectric使用Android版本? Or should I include DecimalFormat in my project (copy/pasting the class from AOSP?)? 还是应该在我的项目中包括DecimalFormat (从AOSP复制/粘贴类?)?

Here's a stripped down example: 这是一个简化的示例:

If I run the App on a device with the following: 如果我在具有以下条件的设备上运行该应用程序:

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

It displays: 它显示:

W/App: 0.0567 W /应用程式:0.0567

In the Unit tests: 在单元测试中:

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

Running the test will output: 运行测试将输出:

junit.framework.ComparisonFailure: junit.framework.ComparisonFailure:

Expected :0.0567 预期值:0.0567

Actual :@@@0 实际的:@@@ 0

As the official android documentation states, the implementation offered when running your java code in an Android RunTime (ART) differs from the one in a regular JVM. 官方的android文档所述,在Android RunTime(ART)中运行Java代码时提供的实现与常规JVM中提供的实现不同。

RoboElectric does not run tests on a regular android phone or an emulator but on a JVM instead. RoboElectric不在常规的Android手机或仿真器上运行测试,而是在JVM上运行。 This JVM, running on your host system, does not have this modified runtime implementation version of DecimalFormat. 在您的主机系统上运行的此JVM没有经过修改的DecimalFormat运行时实现版本。 Therefore as far as I know the functionality Android offers is not available when running your test code in a JVM. 因此,据我所知,在JVM中运行测试代码时,Android提供的功能不可用。 To be able to use it, run your tests on an android phone, but then you may get rid of RoboElectric for that test anyway but use the offered testing tools . 为了能够使用它,请在android手机上运行测试,但是无论如何,您都可以摆脱RoboElectric的支持,而是使用提供的测试工具

Another logical question: Why do you want to test Android classes? 另一个逻辑问题:为什么要测试Android类? I guess Google already wrote tests for there changes and keeps track of that, so you should no test functionality offered to you by the system you're using . 我猜Google已经针对其中的更改编写了测试并跟踪了这些更改,因此您不应使用所使用的系统提供的测试功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM