Self-Test 9

Wed, Jan. 19, 2011

Exceptions


Today we are going to add exception handling code to a Purse class (similar to SelfTest 4, but modified to use an enumeration for the coins) and develop junit tests for it.

Start with these class definitions: Purse.java  PurseTest.java  Coin.java

Coin is an enumeration class.  It looks a little like a class definition, but you can not create objects of type Coin.  It is used to associate a constant with a value (eg ONE_CENT is associated with the double value 0.01).

Take a look at the Purse class to see how to use the Coin enumeration.

Exercise: 

Now take a look at the PurseTest class. The first test looks like this:

    /**
     * Test the AddCoins method.
     */
    public void testAddCoins()
    {
        Purse myPurse = new Purse();
        assertEquals(0, myPurse.getTotal(), EPSILON);
       
        try
        {
            myPurse.addCoins(Coin.ONE_CENT, 1);
            assertEquals(0.01, myPurse.getTotal(), EPSILON);
           
            myPurse.addCoins(Coin.ONE_CENT, 100);
            assertEquals(1.01, myPurse.getTotal(), EPSILON);
        }
        catch (PurseException e)
        {
            fail("Exception should not be thrown");
        }
    }


An exception should not be thrown because in both calls the count argument is positive (1 and 100).  If an exception gets thrown, it will be caught and cause the test to fail by calling the fail method.

    /**
     * Test the AddCoins method - negative argument.
     */
    public void testAddCoinsNeg()
    {
        Purse myPurse = new Purse();
       
        try
        {
            myPurse.addCoins(Coin.TEN_CENT, -1);
            fail("Exception should have been thrown");
        }
        catch (PurseException e)
        {
            // good, this is what we want
        }

    }