Android单元测试 - assertEqual无法使用Locale值

时间:2017-03-08 12:17:00

标签: android unit-testing locale

我正在尝试编写一个Localization类来转换给定段落中的数字和货币。这是我试图运行的测试用例。

@Test
public void localContentTest() throws Exception {
    String content = "Go Square Wakad, 411057 Pune";
    String expectedContent = "Go Square Wakad, 411057 Pune";
    String actualString = Localization.localeContent(content, Locale.GERMAN);
    assertEquals(expectedContent, actualString);
}

Localization.localeContent是返回字符串的简单函数。

 public static String localeContent(final String content, final Locale locale) {
        final StringBuilder stringBuilderResult = new StringBuilder();
        ... 
        //Something to extract number and get it localize.

        return stringBuilderResult.toString();
    }

在测试输出中我得到了这个。我不清楚的是,即使预期和实际相同,测试也会失败。

junit.framework.ComparisonFailure: 
Expected :Go Square Wakad, 411057 Pune
Actual   :Go Square Wakad, 411057 Pune 
 <Click to see difference>


    at junit.framework.Assert.assertEquals(Assert.java:100)
    at junit.framework.Assert.assertEquals(Assert.java:107)
    at localizationdemo.com.localizationdemo.LocalizationTest.localContentTestForNoDecimalValues(LocalizationTest.java:333)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

1 个答案:

答案 0 :(得分:2)

您的实际字符串末尾似乎有一个空格:

Expected :"Go Square Wakad, 411057 Pune"
Actual   :"Go Square Wakad, 411057 Pune "

如果没有,请与我们分享您用来生成实际字符串的代码。