I’m writing a JUnit test method for a Clock class to ensure that the toString representation of the object returns in the format that I want it to. So, I’ve overriden the toString() and written it as it want it to be represented but when I compare it with the format I expect in JUnit it fails with the following:
org.junit.ComparisonFailure: expected: < Time : 10:0[0:0]0 > but was:< Time : 10:0[:]0>
Why is the symbols [ and ] being displayed here? Is it some part of the toString() representation which I do not know about? Relevant code is below:
JUnit:
@Test
public void testFormattingOfTimeIsDisplayedCorrectly() {
final byte TEN = 10;
final byte DBL_ZERO = 00;
clock.setTime(TEN, DBL_ZERO, DBL_ZERO);
final String EXPECTED_STRING = "Time : 10:00:00";
assertEquals(EXPECTED_STRING, clock.toString());
}
toString() in Clock class:
@Override
public String toString() {
return "Time : " + hours + ":" + minutes + ":" + seconds;
}
and the setTime method also in Clock class:
public void setTime(byte hour, byte minutes, byte seconds) {
this.hours = hour;
this.minutes = minutes;
this.seconds = seconds;
}
I’m wondering, maybe it has something to do with using byte? Or is it just something with what toString() returns that I don’t understand. I’m just puzzled about why my JUnit method don’t see them as the same format.
Replace by the line: