JUnit assertEquals 更改字符串
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
方括号强调预期字符串和实际字符串之间的差异。
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.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.
要删除方括号,您应该使实际字符串与预期结果兼容。为此,您需要删除多余的空格或接近方括号的新行空格。
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.