【问题标题】:Java Localized test fails on JenkinsJenkins 上的 Java 本地化测试失败
【发布时间】:2013-10-23 13:47:45
【问题描述】:

我们正在测试我们软件的一些功能,其中一些功能正在使用区域设置。 因此,当我们在我们的机器上进行测试时,所有测试都会运行,但在 Jenkins 中,一些使用语言环境(java 语言环境)的测试会失败。

这是一个例子:

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

} 

请注意,它设置为使用正确的语言环境进行测试。 就像我说的,在我的机器上测试运行没有问题,但在詹金斯它失败了:

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)

编辑:

我在服务器上手动运行测试。我在 CentOS 上使用 yum 安装了 jenkins,所以,Jenkins 有自己的用户。如果我使用服务器的默认登录用户运行测试,则一切正常。但是,当我登录到 jenkins 用户时,测试失败了。我认为这不是詹基斯或代码。但我看不出区别在哪里。 有什么帮助吗?

提前致谢。

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

【问题讨论】:

  • 似乎没有考虑语言环境。 Jenkins 中的 Locale 是如何配置的?
  • 我认为对于 jenkins 语言环境来说不是问题,因为测试设置了要运行的所需语言环境。在这种情况下是“es-CL”。
  • defaultCurrencyFormatter 是如何创建的?

标签: java maven jenkins


【解决方案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 都不是您的测试方法的一部分,因此它们可能存在多线程问题。他们也可能在实例化/实现中有错误。将两者的实例化移动到测试方法本身,不要为它们使用类变量。如果没有帮助,也向我们展示他们的代码。
  • chilocale 它是Locale chileLocale = new Locale("es", "CL");。另一个类只是为不支持的货币做一些智利格式的代码。但是,同样,问题可能出在服务器而不是测试上。这些测试在每台开发人员机器上运行,但不在服务器上。我手动运行作业(-U clean install)并且测试没有失败。
猜你喜欢
  • 2015-03-26
  • 2016-05-23
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2016-10-07
  • 2021-06-02
相关资源
最近更新 更多