Using JUnit to test a method that reverses a singly linked list

Viewed 3404

I'm implementing a singly linked list in Java. I've created a method called reverseList which reverses my list, for example:

head -> 1 -> 2 -> 3 -> null
Becomes:
head -> 3 -> 2 -> 1 -> null

I've also created a test class (using JUnit) that has a method called testReverseList which tests the reverseList method. The insert method takes the data and also the position in which you want to add a new node. All these three methods seems to be working correctly.

public String reverseList() {
    Node prev = null, current = head, next;
    StringBuilder checkList = new StringBuilder();
    while (current != null) {
        checkList.append(current.getData());
        next = current.getNext();
        current.setNext(prev);
        prev = current; 
        current = next;
    }
    head = prev;
    return checkList.toString(); 
}

@Test
public void testReverseList() {
    LinkedList myList = new LinkedList();
    myList.insert(1, 1);
    myList.insert(2, 2);
    myList.insert(3, 3);
    String normalListOrder = "123";
    assertEquals("Check reverseList, reversing a list with 3 elements.", normalListOrder, myList.reverseList());    
}

My question is: I don't believe that I'm properly testing my reverseList method by using this approach of appending each data of my node and then returning it, because I'm not really checking if it was reversed or not. Furthermore, it looks like it would require more computational resources to do all this. And I don't think that just returning a true at the end of execution would be enough either. So, should I don't test it at all? Is there a better way to do the testing?

ANSWER: Thanks everyone. I've followed Pelocho suggestion and overridden the equals method and changed my testReverseList and reverseList methods. I also created testEquals method to check the implemented equals. It seems that this is the best approach for what I want. All the tests are OK.

public LinkedList reverseList() {
    Node prev = null, current = head, next;
    while (current != null) {
        next = current.getNext();
        current.setNext(prev);
        prev = current;
        current = next;
    }
    head = prev;
    return this;
}

public boolean equals(LinkedList myList) {
    if (myList == this) {
        return true;
    }
    if (!(myList.getListSize() == this.getListSize())) {
        return false;
    }
    // Starts at 1 because retrieveNode pick the position, not index
    for (int i = 1; i != this.getListSize(); i++) {
        if (myList.retrieveNode(i).getData() != this.retrieveNode(i).getData()) {
            return false;
        }
    }
    return true;
}

@Test
public void testReverseList() {
    LinkedList myList = new LinkedList();
    myList.insert(1, 1);
    LinkedList singleElementList = new LinkedList();
    singleElementList.insert(1, 1);
    assertEquals("Check reverseList, reversing a list with 1 element.", true, myList.reverseList().equals(singleElementList));  
    LinkedList expectedList = new LinkedList();
    myList.insert(2, 2);
    myList.insert(3, 3);
    expectedList.insert(3, 1);
    expectedList.insert(2, 2);
    expectedList.insert(1, 3);
    assertEquals("Check reverseList, reversing a list with 3 elements.", true, myList.reverseList().equals(expectedList));  
}

public void testEquals() {
    LinkedList myList = new LinkedList();
    myList.insert(1, 1);
    assertEquals("Check reverseList, comparing to the same list.", true, myList.reverseList().equals(myList));
    LinkedList myList2 = new LinkedList();
    myList2.insert(1, 1);
    myList2.insert(2, 2);
    assertEquals("Check reverseList, different size list.", false, myList.reverseList().equals(myList2));
}
3 Answers
Related