java DateTimeFormatterBuilder在测试时失败

我有一个简单的DateTimeFormatterBuilder jUnit测试。 在运行时它可以工作,当一些String出现在Spring-MVC hanlder( @RequestParam )上

在测试时,它失败并具有相同的String值。

测试值: 25-May-2018 11:10

待测方法:

 public void getTimeDifference(@RequestParam String startDate, @RequestParam String endDate) { DateTimeFormatter DATE_TIME_FORMAT = new DateTimeFormatterBuilder().parseCaseInsensitive().appendPattern("dd-MMM-yyyy HH:mm").toFormatter(); LocalDateTime.parse(startDate,DATE_TIME_FORMAT); return messages; } 

测试方法:

 @Test public void testFormat() throws Exception { final String startDateFormatA = "25-May-2018 11:10"; final String endDateFormatA = "25-May-2018 11:10"; assertEquals("06:00", callDbController.getTimeDifference(startDateFormatA, endDateFormatA)[1]); } 

我的测试:在运行时我设置一个断点并在Display-View上测试它:

 LocalDateTime.parse("25-May-2018 11:10",DATE_TIME_FORMAT) 

在具有相同spring-aplication-context的测试时,我在运行时也会这样做,但它失败了。

有没有人有想法?

月份名称是英文,因此您最好在格式化程序中设置java.util.Locale

如果未设置,格式化程序将使用JVM默认语言环境。 如果它不是英语,您可能会收到错误(不同的环境可能有不同的配置,因此最好设置语言环境而不是依赖于JVM的默认设置)。

只做toFormatter(Locale.ENGLISH)而不仅仅是toFormatter()就是这样。