Kevin Bowersox
Kevin Bowersox

Reputation: 94469

JUnit assertEquals Changes String

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.

Upvotes: 9

Views: 6823

Answers (3)

Kanisha Modi
Kanisha Modi

Reputation: 49

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.

Upvotes: 0

Jon7
Jon7

Reputation: 7215

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.

Upvotes: 4

Luke Woodward
Luke Woodward

Reputation: 64959

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.

Upvotes: 16

Related Questions