Writing test for my Java class

I am taking an computer algorithm course taught using Java language. As the course going on, I have to write more and more Java classes . A lot of Classes (files) is not a problem actually, the problem is every class is getting more and more complex and I can’t verify if my implementation is correct at a glance. This put me in an anxious mood which I really hate.

Since I really want to have confidence on my implementation of the class, I resort to the golden TDD method, even though I have never do it before seriously.

I start doing TDD with the LinkedStackOfString.java class.

This class have 3 public apis:

  1. public void push()
  2. public String pop()
  3. public boolean isEmpty()

The implementation is here:

public class LinkedStackOfString {
	private Node first = null;

	private class Node {
		String item;
		Node next;
	}
	
	public boolean isEmpty() {
		return first == null;
	}
	
	public void push(String item) {
		Node oldFirst = first;
		first = new Node();
		first.item = item;
		first.next = oldFirst;
	}
	
	public String pop() {
		String s = first.item;
		first = first.next;
		return s;
	}
}

Does my code work as expect ? I ask myself after complete the implementation. Actually, I am not sure if my code will work as expect.

It’s time to introduce the Test. First let me write my 5 expectation about how my code will works:

  1. This class can be used to initialize an instance without error
  2. Return true by calling isEmpty() of a freshly created instance
  3. Return false by calling isEmpty() after calling .push(String item) of instance
  4. Throw java.lang.NullPointerException by calling pop() of a freshly created instance
  5. Can call .push(String item) without error
  6. Return the latest pushed string by calling pop() (This is FILO)

Then, transforming my exceptions to test code:

import static org.junit.Assert.*;
import org.junit.*;

public class LinkedStackOfStringTest {
	private LinkedStackOfString stack;
	
	@Before
	public void setUp() {
		this.stack = new LinkedStackOfString();
	}
	
	 @After
	public void tearDown() {
		this.stack = null;
	}

	// 1. This class can be used to initialize  an  instance without error
	@Test 
	public void LinkedStackOfString() {
		LinkedStackOfString stack = new LinkedStackOfString();
		assertNotNull("Should initialized without error", stack);
	}

	// 2. Return true by calling isEmpty() of a freshly created instance
	// 3. Return false by calling isEmpty() after calling .push(String item) of instance
	@Test
	public void isEmptyTest() {
		boolean isEmpty = this.stack.isEmpty();
		assertEquals(isEmpty, true);
		this.stack.push("S");
		isEmpty = this.stack.isEmpty();
		assertEquals(isEmpty, false);
	}
	
	// 4. Throw java.lang.NullPointerException by calling pop() of a freshly created instance
	@Test(expected = NullPointerException.class)
	public void popExceptionTest() {
		this.stack.pop();
	}
	
	// 5. Calling .push(String item) without error
	@Test
	public void pushTest() {
		this.stack.push("S");
	}
	
	// 6. Return the latest pushed string by calling pop() (This is FILO)
	@Test
	public void popTest() {
		this.stack.push("S");
		String popedString = this.stack.pop();
		assertEquals(popedString, "S");
	}
	

}

Happy testing result

After some configuration in Eclipse editor I got the testing result. The all-pass-green-color makes me feel pretty good. I know my code work as expect now.

JUnit test all pass result

More about test in java:

  1. Java 8 - Beyond the Basics Tutorial Basic Unit Testing In Java With J-Unit 4 - YouTube
  2. JUnit Assertion
  3. JUnit Expected Exceptions