import org.junit.*; import static org.junit.Assert.*; // This is a skeleton that you can copy and modify to // create a jUnit test class. // Rename this class by substituting the name of your class for "ClassName" // throughout this file. // // Naming convetions. // Given a class named "ClassName", we create a test class // called "TestClassName". Ideally, we write one test member // function for each member funtion of MyClass. In practice, // it is sometimes necessary to test 2 or 3 member functions at a // time. But the key idea is Baby Steps. Write one or two member // functions and the test member function to *thoroughly* test them. // Once you are completely certain that everything is working and // tested, move on to the next one or two member fucntions. // // The class being tested should not contain a "main" member funtion. // // Each test member function should produce as little output as possible. // A common pattern is the following Implementation Plan // a. In MyClass, implement a default constructor and toString(). // In TestMyClass, write testToString that creates an default object // (usually empty) and prints it with toString(). // b. In MyClass, implement a "add" member function. For different classes, // the name of the function will be different, but the idea is that step 'a' // created an empty object, now we need to put some data in it. // In TestMyClass, write testAdd that adds and prints out (using toString()) // several items of data. // c. For most or all the remaining tests, produce no output. Check that the // object is in a correct state by using member functions such as: // isEmpty(), size(), get - to verify what has been placed in the object, etc. // The example of jUnit testing will, hopefully, make this clearer. // // jUnit has a number of assert methods. The most commonly used are: // assertTrue( boolean ), assertFalse( boolean ), assertNull( Object ), // assertNotNull( Object ), fail( String ). // // You can check that your code properly throws exceptions as in this example. // public void testMemberFunctionException() throws NullPointerException { // ClassName cls = null; // try { // cls.memberFunction(); // Should throw an exception // fail( "memberFunction does not throw exception" ); // } catch( NullPointerException e ) {} // } // public class TestClassName { // Fixtures here: methods to initialize any common // objects during each test. // (Optional) @Before @After // Individual test routines here. @Test public static void main( String[] args ) { org.junit.runner.JUnitCore.main("TestClassName"); } }