With lack of real time experience in developing a Java application, I would like to understand the testing strategy for any written module made of multiple classes. Below is the sample application that i learnt from a training, /* SList.java *//** * The SList class is a singly-linked list implementation of the linked list * abstraction. SLists are mutable data structures, which can grow at either * end.  * @author mohet01 * */public class SList{    private SListNode head;    private int size;    /**     * SList() constructs an empty list.     */    public SList(){        this.head = null;        this.size = 0;    }    /**     * isEmpty() indicates whether the list is empty.     * @return true is the list is empty, false otherwise.     */    public boolean isEmpty(){        return size == 0;    }    /**     * length() returns the length of this list.     * @return the length of the list.     */    public int length(){        return size;    }    /**     * insertFront() inserts item obj at the beginning of this list.     * @param obj the item to be inserted.     */    public void insertFront(Object obj){        this.head = new SListNode(obj, this.head);        this.size++;    }    /**     * insertEnd() inserts item obj at the end of this list.     * @param obj the item to be inserted.     */    public void insertEnd(Object obj){        if(head == null){            this.head = new SListNode(obj);        }else{            SListNode node = this.head;            while(node.next != null){                node = node.next;            }            node.next = new SListNode(obj);        }        size++;    }    /**     * nth() returns the item at the specified position. If position < 1 or     * position > this.length(), null is returned. Otherwise, item at      * position position is returned. The list does not change.     * @param position the desired position, from 1 to length(), in the list.     * @return the item at the given position in the list.     */    public Object nth(int position){        if((position < 1) || (position > this.length()) || this.head == null){            return null;        }else{            SListNode currentNode = this.head;            while(position > 1){                currentNode = currentNode.next;                if(currentNode == null)                    return null;                position--;            }            return currentNode.item;        }    }    /**     * toString() converts the list to String.     * @return a string representation of the list.     */    @Override    public String toString() {        Object obj;        String result = [ ;        SListNode currentNode = head;        while(currentNode != null){            obj = currentNode.item;            result = result + obj.toString() +  ;            currentNode = currentNode.next;        }        result = result +  ];        return result;    }    /**     * main() runs test cases on SList class. Prints summary     * information on basic operations and halts with an error (and a stack     * trace) if any of the tests fail.      * @param args     */    public static void main(String[] args){        testEmpty();        testAfterInsertFront();        testAfterInsertEnd();    }    /**     * testEmpty() tests toString(), isEmpty(), length(), insertFront(), and     * insertEnd() on an empty list. Prints summary information of the tests      * and halts the program if errors are detected.     */    private static void testEmpty(){        SList lst1 = new SList();        SList lst2 = new SList();        System.out.println();        lst1.toString();        System.out.println(Here is a list after construction:                  + lst1.toString());        TestHelper.verify(lst1.toString().equals([  ]),                  toString on newly constructed list failed);        System.out.println(isEmpty() should be true. It is:  +                   lst1.isEmpty());        TestHelper.verify(lst1.isEmpty() == true,                  isEmpty() on newly constructed list failed);            System.out.println(length() should be 0. It is:  +                   lst1.length());        TestHelper.verify(lst1.length() == 0,                   length on newly constructed list failed);            lst1.insertFront(new Integer(3));        System.out.println(Here is a list after insertFront(3) to an empty list:                    + lst1.toString());        TestHelper.verify(lst1.toString().equals([  3  ]),                  InsertFront on empty list failed);        lst2.insertEnd(new Integer(5));        System.out.println(Here is a list after insertEnd(5) on an empty list:                    + lst2.toString());        TestHelper.verify(lst2.toString().equals([  5  ]),                  insertEnd on empty list failed);    }    /**     * testAfterInsertFront() tests toString(), isEmpty(), length(),     * insertFront(), and insertEnd() after insertFront(). Prints summary     * information of the tests and halts the program if errors are detected.      */    private static void testAfterInsertFront(){        SList lst1 = new SList();        lst1.insertFront(new Integer(3));        lst1.insertFront(new Integer(2));        lst1.insertFront(new Integer(1));        System.out.println();        System.out.println(Here is a list after insertFront 3, 2, 1:                 + lst1.toString());        TestHelper.verify(lst1.toString().equals([  1  2  3  ]), InsertFronts on non-empty list failed);        System.out.println(isEmpty() should be false. It is:  +                lst1.isEmpty());        TestHelper.verify(lst1.isEmpty() == false,                 isEmpty() after insertFront failed);        System.out.println(length() should be 3. It is:  +                lst1.length());        TestHelper.verify(lst1.length() == 3,                 length() after insertFront failed);        lst1.insertEnd(new Integer(4));        System.out.println(Here is the same list after insertEnd(4):                    + lst1.toString());        TestHelper.verify(lst1.toString().equals([  1  2  3  4  ]),                  insertEnd on non-empty list failed);    }    /**       *  testAfterInsertEnd() tests toString(), isEmpty(), length(),       *  insertFront(), and insertEnd() after insertEnd().  Prints summary       *  information of the tests and halts the program if errors are detected.       **/      private static void testAfterInsertEnd() {        SList lst1 = new SList();        lst1.insertEnd(new Integer(6));        lst1.insertEnd(new Integer(7));        System.out.println();        System.out.println(Here is a list after insertEnd 6, 7:                    + lst1.toString());        System.out.println(isEmpty() should be false. It is:  +                   lst1.isEmpty());        TestHelper.verify(lst1.isEmpty() == false,                  isEmpty() after insertEnd failed);            System.out.println(length() should be 2. It is:  +                   lst1.length());        TestHelper.verify(lst1.length() == 2,                   length() after insertEndfailed);        lst1.insertFront(new Integer(5));        System.out.println(Here is the same list after insertFront(5):                    + lst1.toString());        TestHelper.verify(lst1.toString().equals([  5  6  7  ]),                  insertFront after insertEnd failed);      }}/* TestHelper.java *//** * This class is based on code from Arnow/Dexter/Weiss. Its verify() method  * exits with an error message if an invariant fails to hold true. *  * The purpose of this class is to provide a shorthand for writing and testing  * invariants in any program. * */public class TestHelper{    static void verify(boolean invariant, String message){        if(!invariant){            System.out.println(*** ERROR:   + message);            Thread.dumpStack();        }    }}where a list abstraction SList is being unit tested with below 3 callstestEmpty();testAfterInsertFront();testAfterInsertEnd();My question:Does it take similar approach to unit test the module that is written by developer in any real time application? Based on above code, Do you think unit testing strategy can be made better?Note: Please let me know, if SListNode class has to be shown as part of this query.
How to write unit test cases for any real time Java app?
java;unit testing
Ok, here's my slightly more fleshed out answer after my comment...Just to repeat: there are testing frameworks/utilities like JUnit, TestNG and Hamcrest matchers, so that you do not need to roll your own TestHelper class.You don't have to write boolCondition == true or boolCondition == false, to test for true conditions, they can simply be expressed as boolCondition and !boolCondition respectively.Asserting via the toString() representation may look like it works now, but the proper way is to override your class's equals() method (and as a form of habit, hashCode() too). Your toString() representation may change in the future, which means you'll have to update your test cases.Test cases usually belong to their own class, e.g. SListTest.Why do you need to dumpStack() when only the assertion is false? Maybe I only accidentally compared 1 == 2, and I'm not expecting any stack dump...