Home » DS02 SingleLinkedList TestNG

Share This Post

Tutorial

DS02 SingleLinkedList TestNG

Testing SingleLinkedList with TestNG Framework

This post is a continuation of the prev post DS01 SingleLinkedList and Iterator

TestNG Unit Testing

NG stands for Next Generation. So I guess JUnit is like Star Trek Original Series and TestNG is like Star Trek Next Generation! From what I have seen thus far, it’s pretty darn good though. So the two failed test I was getting were, NullPointerExceptions and failed assertions. I only used assertTrue() assertFalse() assertNull() assertNotNull() and assertEquals(). There are plenty more types to use in the test suite.

I do not believe that I can be a purist in test first methodology.  I discovered something in testing these classes. Don’t write a lot of code and then a lot of test. I feel that you should write a little code, then a little bit of testing. And/or a little bit of testing then some code. Do short burst. Code some more then go back and make the prior test a bit more thorough. So [code][test][code][test][test][code][code][test][code][test] etc. Not [code][code][code][code][test][test][test][test] or [test][test][test][test][code][code][code][code]. Get it?

I coded it all then I tested it. I had more bugs and errors in logic than I would have thought I should have had. If you test as you code and code as you test bugs and errors in logic will show up quickly. You may even not introduce these problems in the first place. So it helps in working out the logic. It took quite a bit more time to test the class (while re-coding) than the initial coding.

Node class Test

This was like the Node class implementation, simple. Simple implementation or class and you also have simple testing most likely. So there is not much to say here.

/*
 * CPL Common Public License
 */
package sdz.ds.lists;

import static org.testng.Assert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 * 
 * @author Larry Gray
 */
public class NodeNGTest {
 
 /**
  *
  */
 public NodeNGTest() {
 }

 /**
  *
  * @throws Exception
  */
 @BeforeClass
 public static void setUpClass() throws Exception {
 }

 /**
  *
  * @throws Exception
  */
 @AfterClass
 public static void tearDownClass() throws Exception {
 }

 /**
  *
  * @throws Exception
  */
 @BeforeMethod
 public void setUpMethod() throws Exception {
 }

 /**
  *
  * @throws Exception
  */
 @AfterMethod
 public void tearDownMethod() throws Exception {
 }

 /**
  * Test of getElement method, of class Node.
  */
 @Test
 public void testGetElememnt() {
  System.out.println("getElement");
  Node instance = new Node();
  instance.setElement(new Integer(5));
  Integer expResult = new Integer(5);
  Integer result = (Integer)instance.getElement();
  assertEquals(result, expResult);
 
 } // testGetElement

 /**
  * Test of getNext method, of class Node.
  */
 @Test
 public void testGetNext() {
  System.out.println("getNext");
  Node instance = new Node();
  Node instance2 = new Node();
  instance.setNext(instance2);
  Node result = instance.getNext();
  assertEquals(result, instance2);
  
 } // testGetNext

 /**
  * Test of setElement method, of class Node.
  */
 @Test
 public void testSetElement() {
  System.out.println("setElement");
  Integer element = new Integer(5);
  Node instance = new Node();
  instance.setElement(element);
  assertEquals(instance.getElement(),new Integer(5));
  
 } // testSetElement

 /**
  * Test of setNext method, of class Node.
  */
 @Test
 public void testSetNext() {
  System.out.println("setNext");
  Node next = new Node();
  Node instance = new Node();
  instance.setNext(next);
  assertEquals(instance.getNext(),next);
  
 } // testSetNext
} // NodeNGTest class

Test Single Linked List Iterator

Cases tested were, adding and removing or deleting to/from head, middle and tail of list. Also adding to empty list, one item list, two item list and multiple item list. Method size() was tested a lot throughout all test. Iterator methods next() and hasNext() were tested in the same way.

I thought of something I did not test. If in the midst of navigating when you remove head or tail over and over until the node at position is head or tail. I may have handled this to some extent. But to be sure this is working properly I need to test it. Below are some exerpts from the source. Full source is at TestNG SingleLinkedList Class

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sdz.ds.lists;

import static org.testng.Assert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 *
 * @author larry
 */
public class SingleLinkedListNGTest {

 SingleLinkedList testList1 = new SingleLinkedList();

 public SingleLinkedListNGTest() {

 }

 @BeforeClass
 public static void setUpClass() throws Exception {

 }

 @AfterClass
 public static void tearDownClass() throws Exception {
 }

 @BeforeMethod
 public void setUpMethod() {
  testList1 = new SingleLinkedList();
  testList1.insertHead(3);
  testList1.insertHead(2);
  testList1.insertHead(1);
 }

 @AfterMethod
 public void tearDownMethod() throws Exception {
 }

 /**
  * Test of insertHead method, of class SingleLinkedList.
  */
 @Test
 public void testInsertHead() {
  System.out.println("insertHead");
  testList1 = new SingleLinkedList();
  // new list insert
  testList1.insertHead(3);
  assertEquals(testList1.size(), 1);
  assertEquals(testList1.get(), 3);
  assertEquals(testList1.head(), 3);
  assertEquals(testList1.tail(), 3);
  // 2nd item list insert
  testList1.insertHead(2);
  assertEquals(testList1.size(), 2);
  assertEquals(testList1.get(), 2);
  assertEquals(testList1.head(), 2);
  assertEquals(testList1.tail(), 3);
  // 3rd item list insert
  testList1.insertHead(1);
  assertEquals(testList1.size(), 3);
  assertEquals(testList1.get(), 1);
  assertEquals(testList1.head(), 1);
  assertEquals(testList1.tail(), 3);
 } // testInsertHead

 /**
  * Test of insertTail method, of class SingleLinkedList.
  */
 @Test
 public void testInsertTail() {
  System.out.println("insertTail");
  testList1 = new SingleLinkedList();
  // new list insert
  testList1.insertTail(1);
  assertEquals(testList1.size(), 1);
  assertEquals(testList1.get(), 1);
  assertEquals(testList1.head(), 1);
  assertEquals(testList1.tail(), 1);
  // 2nd item list insert
  testList1.insertTail(2);
  assertEquals(testList1.size(), 2);
  assertEquals(testList1.get(), 1);
  assertEquals(testList1.head(), 1);
  assertEquals(testList1.tail(), 2);
  // 3rd item list insert
  testList1.insertTail(3);
  assertEquals(testList1.size(), 3);
  assertEquals(testList1.get(), 1);
  assertEquals(testList1.head(), 1);
  assertEquals(testList1.tail(), 3);

 } // testInsertTail

 /**
  * Test of removeHead method, of class SingleLinkedList.
  */
 @Test
 public void testRemoveHead() {
  System.out.println("removeHead");
  setUpMethod();
  // test with more than 2 in list
  Integer theInt;
  theInt = (Integer) testList1.removeHead();
  assertEquals(theInt.intValue(), 1);
  assertEquals(((Integer) testList1.head()).intValue(), 2);
  // test with 2 in list
  theInt = (Integer) testList1.removeHead();
  assertEquals(theInt.intValue(), 2);
  assertEquals(((Integer) testList1.head()).intValue(), 3);
  assertEquals(((Integer) testList1.tail()).intValue(), 3);
  assertEquals(((Integer) testList1.get()).intValue(), 3);
  // test with 1 in list
  theInt = (Integer) testList1.removeHead();
  assertEquals(theInt.intValue(), 3);
  assertNull(testList1.head());
  assertNull(testList1.tail());
  assertNull(testList1.get());
  assertEquals(testList1.size(), 0);
  // empty list

  theInt = (Integer) testList1.removeHead();
  assertNull(theInt);
 } // testRemoveHead

 /**
  * Test of removeTail method, of class SingleLinkedList.
  */
 @Test
 public void testRemoveTail() {
  System.out.println("removeTail");
  setUpMethod();
  // test with more than 2 in list
  Integer theInt;
  theInt = (Integer) testList1.removeTail();
  assertEquals(theInt.intValue(), 3);
  assertEquals(((Integer) testList1.tail()).intValue(), 2);
  // test with 2 in list
  theInt = (Integer) testList1.removeTail();
  assertEquals(theInt.intValue(), 2);
  assertEquals(((Integer) testList1.head()).intValue(), 1);
  assertEquals(((Integer) testList1.tail()).intValue(), 1);
  assertEquals(((Integer) testList1.get()).intValue(), 1);
  // test with 1 in list
  theInt = (Integer) testList1.removeHead();
  assertEquals(theInt.intValue(), 1);
  assertNull(testList1.head());
  assertNull(testList1.tail());
  assertNull(testList1.get());
  assertEquals(testList1.size(), 0);
  // empty list
  theInt = (Integer) testList1.removeHead();
  assertNull(theInt);
 } 

 /**
  * Test of reset method, of class SingleLinkedList.
  */
 @Test
 public void testReset() {
  System.out.println("reset");
  SingleLinkedList testList1 = new SingleLinkedList();
  // test empty
  testList1.reset();
  // insert 3
  testList1.insertHead(3);
  testList1.insertHead(2);
  testList1.insertHead(1);

  testList1.next();
  testList1.next();

  testList1.reset();
  assertEquals(((Integer) testList1.head()).intValue(), 1);
  assertNotNull(testList1.head());
  assertNotNull(testList1.tail());
 } // testReset

 /**
  * Test of next method, of class SingleLinkedList.
  */
 @Test
 public void testNext() {
  System.out.println("next");
  SingleLinkedList testList1 = new SingleLinkedList();
  // empty
  testList1.next();
  // 1 item in list
  testList1.insertHead(3);
  testList1.reset();
  testList1.next();
  assertEquals(testList1.get(), null);
  // 2 items in list
  testList1.insertHead(2);
  testList1.insertHead(1);
  boolean exception = false;

  assertNotNull(testList1.head());
  testList1.next();
  assertEquals(((Integer) testList1.get()).intValue(), 2);
  testList1.next();
  assertEquals(((Integer) testList1.get()).intValue(), 3);
  testList1.next();
  assertNull(testList1.get());

 } // testNext

 /**
  * Test nextElementMethod() for front of list, mid list and end list.
  */
 @Test
 public void testNextElement() {
  System.out.println("nextElement");
  SingleLinkedList testList1 = new SingleLinkedList();
  boolean notEOL = false;
  // empty
  notEOL = testList1.nextElement();
  assertFalse(notEOL);
  // 1 item
  notEOL = false;
  testList1.insertHead(3);
  testList1.reset();
  notEOL = testList1.nextElement();
  assertFalse(notEOL);

  // 2 items
  testList1.insertHead(2);
  testList1.insertHead(1);
  testList1.reset();
  testList1.nextElement();
  // 3 items
  notEOL = false;
  testList1.reset();
  assertNotNull(testList1.head());
  notEOL = testList1.nextElement();
  assertTrue(notEOL);
  assertEquals(((Integer) testList1.get()).intValue(), 2);
  notEOL = testList1.nextElement();
  assertTrue(notEOL);
  assertEquals(((Integer) testList1.get()).intValue(), 3);
  notEOL = testList1.nextElement();
  assertFalse(notEOL);
  assertNull(testList1.get());
 } // testNextElement

 

Finally

Testing can seem boring but I actually began to enjoy it as I got into it. I look forward now to improving on this implementation and working on the next data structures in the series. I have no good working examples in this article for using these classes. The next articles in the series will be first DoubleLinkedList and then Stacks and Queues. Stacks and queues will be an example for using this class. Both stacks and queues do  not need a double linked list. I hope you enjoyed the first in this series on data structures.

Share This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>