在Jenkins上Java本地化测试失败

3

我们正在测试软件的一些功能,其中一些功能需要使用Locale。 因此,在我们的机器上测试时,所有测试都可以通过,但在Jenkins上,一些使用Locale(Java locale)的测试失败了。

以下是一个例子:

@Test
public void testCurrencyFormat_withCLPFormat_returnValidFormat() {

    BigDecimal amount = new BigDecimal("500456.789");
    java.util.Currency currency = java.util.Currency
            .getInstance(chileLocale);
    Currency c = new Currency(currency);

    assertEquals("500.456,789",
            defaultCurrencyFormatter.format(c, amount, chileLocale));

} 

请注意,区域设置是为了使用正确的区域设置进行测试。就像我说的,在我的机器上测试没有问题,但在Jenkins上失败了。
org.junit.ComparisonFailure: expected:<500[.456,]789> but was:<500[,456.]789>
at org.junit.Assert.assertEquals(Assert.java:125)
at org.junit.Assert.assertEquals(Assert.java:147)
at cl.taisachile.antaios.domain.currency.CurrencyTest.testCurrencyFormat_withCLPFormat_returnValidFormat(CurrencyTest.java:93)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:107)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)

编辑:

我在服务器上手动运行测试。我使用yum在CentOS上安装了jenkins,所以Jenkins有自己的用户。如果我使用默认登录用户登录服务器运行测试,一切都正常。但是,当我登录到jenkins用户时,测试失败了。我认为这不是jenkis或代码的问题。但我不知道区别在哪里。 需要帮助吗?

提前致谢。

编辑2: 仅作记录:我们更改了服务器的设置,错误消失了。 我们测试了几种配置,但无法确定为什么在centos/32位上会失败。 感谢大家。


看起来地区设置没有被考虑进去。Jenkins中的Locale是如何配置的? - Olivier.Roger
我认为 Jenkins 的本地化不是问题,因为测试设置了要运行的所需区域设置。在这种情况下,它是“es-CL”。 - dodoconr
defaultCurrencyFormatter 是如何创建的? - Karol S
1个回答

1
也许这是测试(Jenkins)机器上安装的Java版本的问题,也可能是你的代码的问题。
你没有展示 chileLocale 是如何实例化的,可能存在问题。 同时,你没有展示正在测试的 defaultCurrencyFormatterCurrency 对象的代码。
这个测试应该展示 es-CL 区域设置的小数点和千位分隔符的系统定义。
public static void main(String[] args) {
    Locale chileLocale = new Locale("es", "CL");
    DecimalFormat formatter = (DecimalFormat) DecimalFormat.getInstance(chileLocale);

    char sep = formatter.getDecimalFormatSymbols().getDecimalSeparator();
    char grp = formatter.getDecimalFormatSymbols().getGroupingSeparator();

    System.out.println("Separator: " + sep + " Grouping: " + grp);
}

如果它们在不同的系统上有所不同,则您应该查看Java安装,这显然是问题所在。
编辑: 另一条注意事项: 请记住,标准Java NumberFormat及其派生类不是线程安全的。 如果您从不同的线程访问相同的格式化程序实例,则可能会遇到麻烦。 确保每个线程只有一个格式化程序实例。

defaultCurrencyFormatterchileLocale都不是您测试方法的一部分,因此它们可能存在多线程问题的候选项。此外,它们在实例化/实现方面可能存在错误。将两者的实例化移动到测试方法本身中,不要使用类变量。如果这样做不起作用,请向我们展示它们的代码。 - Udo Klimaschewski
本地化是 Locale chileLocale = new Locale("es", "CL");。另一个类只是为不支持的货币提供智利格式的一些代码。但是,也许问题在于服务器而不是测试。这些测试在每个开发者的机器上都可以运行,但在服务器上却不能。我手动运行了作业(-U clean install),测试没有失败。 - dodoconr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接