Search code examples
javastringjunitjunit4

JUnit assertEquals failure shows identical strings ending in []


I have created a program to create and manipulate text lines in the form of character arrays. I am currently in the process of testing it, and running into an issue while using JUnit assertEquals. I am using assertEquals to test methods by comparing the individual values in the TextLine object with what is expected.

For my first few tests, there were not any issues and everything worked, including the overridden toString() method. However, this test fails (a is declared with EditableTextLine a; previously):

@Test
public void testGrowArray() {
    a = new EditableTextLine("iamdoingfine");
    assertEquals("toString", "Text line: iamdoingfine", a.toString());
    assertEquals("length", 12, a.length());
    assertEquals("capacity", 80, a.capacity());
    a.testGrowArray(100);
    assertEquals("toString2", "Text line: iamdoingfine", a.toString());
    assertEquals("length2", 100, a.length());
    assertEquals("capacity2", 160, a.capacity());
}

This failure message is this:

1) testGrowArray(edu.uga.cs1302.test.EditableTextLineTester)
org.junit.ComparisonFailure: toString2 expected:<...t line: iamdoingfine[]> but was:<...t line: iamdoingfine[]>
        at org.junit.Assert.assertEquals(Assert.java:117)
        at edu.uga.cs1302.test.EditableTextLineTester.testGrowArray(EditableTextLineTester.java:93)

Specifically, my problem is expected:<...t line: iamdoingfine[]> but was:<...t line: iamdoingfine[]>. I cannot what the issue is or how I can fix it. It looks like there is an invisible, zero-width character somewhere, but I don't know how to fix it.

toString code:

@Override
public String toString() {
    String out = "Text line: ";
    for (int i = 0; i < length; i++) {
        out += txtLine[i];
    }
    return out;
}

Here is the growArray method (which is currently called by public method testGrowArray(int newLength), which just calls growArray(newLength):

private void growArray(int newLength) {
    //Creates a buffer with a capacity equal to the smallest multiple of
    //the default size capable of holding the original line plus the
    //added spaces
    int multNeeded = (int) Math.ceil( (double)newLength / DEFAULT_SIZE);
    char[] tempBuffer = new char[DEFAULT_SIZE * multNeeded];
    setCapacity(DEFAULT_SIZE * multNeeded);

    for (int i = 0; i < length(); i++) //Writes the orignal line to the buffer
        tempBuffer[i] = this.txtLine[i];
    this.txtLine = tempBuffer; //Sets txtLine to new, larger array
    setLength(newLength);
}

DEFAULT_SIZE is 80, if it matters.

Also, I ran the test without the toString2 assert and it completed without failures, so length() and capacity() both ran properly.

Any help in finding out what is causing the invisible character to appear and why would be greatly appreciated. I have not been able to find any other questions with the same issue, but if there is one please let me know.

EDIT: I have solved my issue; as usual it was caused by my own brainfarts.I want to leave this up for future users, but I cannot select my own answer as a solution for 2 days for whatever reason so it will remain unanswered.

My answer is in my comment if anyone needs it.


Solution

  • Solution/problem was that my toString statement was grabbing the null characters in the array and printing them. Ordinarily, there would be other characters there, but since I skipped that to test growArray, they were not placed.

    To clarify further: my toString uses length() to stop concatenating characters. I added length to the array without putting characters there, so the toString method tried to add characters from array slots that had never been filled, thus resulting in invisible characters.