简体   繁体   English

Java本地化测试在Jenkins上失败

[英]Java Localized test fails on Jenkins

We are testing some features of our software, and some of them are working with a Locale. 我们正在测试软件的某些功能,其中一些功能正在使用区域设置。 So, when we test in our machines, all tests runs, but in Jenkins some test using locale (java locale) fails. 因此,当我们在机器上进行测试时,所有测试都会运行,但是在Jenkins中,使用语言环境(java语言环境)的某些测试会失败。

Here's an example: 这是一个例子:

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

} 

Note that the locale it's setted for use the right locale for the test. 请注意,将其设置为使用正确的语言环境进行测试。 Like i said, in my machine the test run without problems, but in jenkins it fails: 就像我说的那样,在我的机器上测试运行没有问题,但是在詹金斯中,它失败了:

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)

EDIT: 编辑:

I manually run the test on the server. 我在服务器上手动运行测试。 I installed jenkins using yum on CentOS, so, Jenkins has his own user. 我在CentOS上使用yum安装了jenkins,因此,Jenkins有自己的用户。 If i run the test using the default login user to the server, all run's ok. 如果我使用服务器的默认登录用户运行测试,则所有运行正常。 But, when i log in into the jenkins user, the test's fails. 但是,当我登录到jenkins用户时,测试失败。 I think it's not jenkis or the code. 我认为这不是詹基斯或代码。 But i don't see where is the difference. 但是我看不出有什么区别。 Any Help? 有帮助吗?

Thanks in advance. 提前致谢。

EDIT 2: Just for the record: We changed the setup of the server and the errors disapear. 编辑2:仅作记录:我们更改了服务器的设置,并且错误消失了。 We tested several configurations and couldn't determine why fails on centos/32bits. 我们测试了几种配置,无法确定为什么在centos / 32bits上失败。 Thanks for all. 谢谢大家

maybe it is a problem of the Java version installed on the test (Jenkins) machine, maybe it is a problem of your code. 可能是测试(Jenkins)计算机上安装的Java版本有问题,也许是您的代码有问题。

You do not show how the chileLocale is instantiated, there could be a problem. 您没有显示如何实例化chileLocale ,可能存在问题。 Also you do not show the code of your defaultCurrencyFormatter and Currency object, which are under test. 另外,您不会显示正在测试的defaultCurrencyFormatterCurrency对象的代码。

This test should show you the systems definitions for the decimal and group separators for the es-CL locale: 此测试应向您显示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);
}

If they differ on your different systems, then you should have a look at the Java installation, which is then obviously the problem. 如果它们在您的不同系统上不同,那么您应该看看Java安装,这显然是问题所在。

EDIT: Another note: Keep in mind that standard Java NumberFormat and its derivatives are not thread-save. 编辑:另一个注意事项:请记住,标准Java NumberFormat及其派生不是线程保存的。 If you are accessing the same formatter instance from different threads, you might run into trouble. 如果要从不同的线程访问同一格式化程序实例,则可能会遇到麻烦。 Make sure to have single formatter instance per thread. 确保每个线程有一个格式化程序实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM