Jenkins 上的 Java 本地化测试失败

标签 java maven jenkins

我们正在测试我们软件的一些功能,其中一些正在使用区域设置。 因此,当我们在我们的机器上进行测试时,所有测试都会运行,但在 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));

} 

请注意,为测试设置的语言环境使用正确的语言环境。 就像我说的,在我的机器上测试运行没有问题,但在 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)

编辑:

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

提前致谢。

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

最佳答案

可能是测试(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 及其衍生格式不是线程保存的。 如果您从不同的线程访问同一个格式化程序实例,您可能会遇到麻烦。 确保每个线程只有一个格式化程序实例。

关于Jenkins 上的 Java 本地化测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19543504/

相关文章:

java - 奇怪的延迟加载异常

java - 在 Web 应用程序中出现奇怪的错误

http - 在构建作业 jenkins 后运行 CURL 脚本

当所有代理都离线时,Jenkins 管道队列已满

java - 显式、动态加载 Java 字符集

java - 设置固定在 JPanel 中的 JTextField 的大小

scala - pom配置以通过scala maven插件强制使用jvm 7

maven - 尝试使用 Maven Surefire 在不同的 JVM 上运行 JUNIT 测试

java - 创建 Maven 项目,无法解析原型(prototype),连接被拒绝

jenkins - 我可以在 Jenkins 管道脚本中创建新作业吗