JUnit assertEquals 更改字符串

发布于 2024-12-04 00:20:00 字数 778 浏览 0 评论 0原文

我有一个 JUnit 测试,如下所示:

@Test
public void testToDatabaseString() {
  DateConvertor convertor = new DateConvertor();
  Date date = convertor.convert("20/07/1984:00:00:00:00");
  String convertedDate = convertor.toDatabaseString(date);

  assertEquals("to_date('20/07/1984:00:00:00:00', 'DD/MM/YYYY HH24:MI:SS')",convertedDate);
}

测试失败,说明:

org.junit.ComparisonFailure: expected:<to_date('20/07/1984[00:]00:00:00', 'DD/MM/YY...> but was:<to_date('20/07/1984[ ]00:00:00', 'DD/MM/YY...>

特别有趣的是为什么预期值为:

to_date('20/07/1984[00:]00:00:00', 等...

当我在测试中的字符串文字清楚地显示:

"to_date('20/07/1984:00:00:00:00', 等...

任何人都可以解释一下吗?为什么要添加“[00:]”

I have a JUnit test that is as follows:

@Test
public void testToDatabaseString() {
  DateConvertor convertor = new DateConvertor();
  Date date = convertor.convert("20/07/1984:00:00:00:00");
  String convertedDate = convertor.toDatabaseString(date);

  assertEquals("to_date('20/07/1984:00:00:00:00', 'DD/MM/YYYY HH24:MI:SS')",convertedDate);
}

The test fails stating:

org.junit.ComparisonFailure: expected:<to_date('20/07/1984[00:]00:00:00', 'DD/MM/YY...> but was:<to_date('20/07/1984[ ]00:00:00', 'DD/MM/YY...>

Of particular interest is why the expected value is:

to_date('20/07/1984[00:]00:00:00', etc...

when my string literal in the test is clearly:

"to_date('20/07/1984:00:00:00:00', etc...

Can anyone explain this? Why does it add "[00:]"? Appreciate the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糯酥胸 2024-12-11 00:20:00

方括号强调预期字符串和实际字符串之间的差异。

JUnit 将 :00 放在方括号中,以强调这是预期字符串中的内容,而不是实际字符串中的内容。出于同样的原因,实际字符串中的空格周围也有方括号。

The square brackets are emphasising the difference between the expected string and the actual string.

JUnit put the square brackets around the :00 to emphasise that that is what's in the expected string and not in the actual string. There are square brackets around the space in the actual string for the same reason.

始终不够 2024-12-11 00:20:00

JUnit 只是将字符串中不相等的字符放在括号中,以使其更易于阅读。您的断言会查找 4 组“:00”,而您的变量只有 3 组。

正如这个SO问题中所述(Java:assertEquals(String, String)可靠吗? ),assertEquals 只是在您传递给它的对象上调用 .equals 方法。

JUnit is just putting the characters in your string that weren't equal in brackets to make it easier to read. Your assert looks for 4 sets of ":00" and your variable only had 3 sets.

As noted in this SO question (Java: Is assertEquals(String, String) reliable?), assertEquals just calls the .equals method on the objects that you pass it.

腻橙味 2024-12-11 00:20:00

要删除方括号,您应该使实际字符串与预期结果兼容。为此,您需要删除多余的空格或接近方括号的新行空格。

To remove the square bracket, you should make your actual string compatible with the expected result. For that you need to remove the extra space or new line space which is nearer to square bracket.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文