java - Test Android DecimalFormat with Robolectric -
the android library modified decimalformat class nice additions. of course, need 1 of additions (the signifant digit notation: @
).
when i'm testing robolectric, seems standard java decimalformat class used, makes tests fail.
is there way tell robolectric use android version? or should include decimalformat
in project (copy/pasting class aosp?)?
here's stripped down example:
if run app on device 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
in 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 test output:
junit.framework.comparisonfailure:
expected :0.0567
actual :@@@0
as official android documentation states, implementation offered when running java code in android runtime (art) differs 1 in regular jvm.
roboelectric not run tests on regular android phone or emulator on jvm instead. jvm, running on host system, not have modified runtime implementation version of decimalformat. therefore far know functionality android offers not available when running test code in jvm. able use it, run tests on android phone, may rid of roboelectric test anyway use offered testing tools.
another logical question: why want test android classes? guess google wrote tests there changes , keeps track of that, you should no test functionality offered system you're using.
Comments
Post a Comment