E-X-C-E-P-T-I-O-N

Tests that determine whether both a method invocation and a constructor invocation throws the desired exception.

Overview

The tester also allows the user to test whether a method invocation by the given instance and with the given arguments throws the expected exception with the message the programmer expects. The tester will report every type of failure - when the method fails to throw exception, when the method throws and exception of a different type, when the method throws an exception of the correct type, but with a wrong message.

The tester also allows the user to test whether a constructor invocation for the given class and with the given arguments throws the expected exception with the message the programmer expects. The tester will report every type of failure - when the constructor fails to throw exception, when the constructor throws and exception of a different type, when the constructor throws an exception of the correct type, but with a wrong message.

The class ExamplesMethodExceptions and ExamplesConstructorExceptions contains all test cases.

Code sources

Here is the complete source code for this test suite.

You can also download the entire souce code as a zip file.

Complete test results are shown here.

Brief Examples

Here is an example that illustrates such tests.

public class ExamplesMethodExceptions {

    public boolean testExceptions1(Tester t) {
	try {
	    /** A test that throws an exception */
	    new MTListTr().getFirst();
	    return
	    t.checkExpect(
	        false, // condition
	        "new MTListTr().getFirst() failed to throw an exception"); // test case name
	    } catch (UnsupportedOperationException e) {
		//we expect to come here to catch the exception
		return
	        t.checkExpect(
		    true,
		    "new MTListTr().getFirst() threw exception: \n" + e.getMessage());
	    }
    }

    public boolean testExceptions2(Tester t) {
	return
        /** A test that should not throw an exception */
	t.checkException(
	    // test case name (optional)
	    "new MTListTr().isEmpty() \n" +
	    "The test should fail: the method fails to throw an exception",
	    // the expected exception
	    new UnsupportedOperationException(
	        "Cannot access the first element of an empty data set"),

	    // the object to be tested
	    new MTListTr(),

	    // the name of the method to be invoked and tested
	    "isEmpty",

	    // the arguments for the method
	    new Object[0]);
    }
}


public class ExamplesConstructorExceptions {

    String MSG_INVALID_YEAR_IN_DATE = "Invalid year in Date";
    String MSG_INVALID_MONTH_IN_DATE = "Invalid month in Date";
    String MSG_INVALID_DAY_IN_DATE = "Invalid day in Date";

    public boolean testConstructorExceptionIsCaught(Tester t) {
        boolean caught = false;
	try {
	    Date b34453323 = new Date(3445, 33, 23);
	} catch (IllegalArgumentException exc) {
	    // we expect to get here
	    String message = exc.getMessage();

	    // check the exception is due to invalid date
	    if ((message.equals(MSG_INVALID_YEAR_IN_DATE)) ||
	    (message.equals(MSG_INVALID_MONTH_IN_DATE)) ||
	    (message.equals(MSG_INVALID_DAY_IN_DATE)))
	    caught = true;
	}

	// assert caught = true
	return t.checkExpect(
	    caught, // condition
	    "Should be success: constructor exception is caught"); // test name
    }

    public boolean testConstructorException1(Tester t) {
	// create an instance of a class that converts the
	// short String representation of a month into a number
	IMonth2Number ms2n = new MonthShort2Number();

	return
	t.checkConstructorException(
	    // test case name (optional)
	   "Should be success: invalid month for IMonth2Number",

	    // the expected exception
	    new IllegalArgumentException(MSG_INVALID_MONTH_IN_DATE),

	    // the name of the constructor to be invoked and tested
	    "exceptions.Date",

	    // the arguments for the constructor:
	    2000,    // the year
	    "Fbb",   // the month (incorrect)
	    12,      // the day
	    ms2n);     // month-to-number converter instance
    }
}



back

Last updated: March 31, 2011 10:00 am