| Exceptions | |||||||||||||||||||||||||||
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. An example of such test is shown below:
// A test for exceptions
// -- this code is expected to throw an exception of the given type
// but with a different message - and the test should fail
public void testExceptions(Tester t){
/** A test that throws an exception */
t.checkException(
// the test name - information about this test
"new MTListTr().getRest() \n" +
"The test should fail: we supplied a wrong message",
// the expected exception and message
new UnsupportedOperationException(
"No first element in an empty data set"),
// the instance that invokes the method
new MTListTr<Object>(),
// the method name
"getRest"
// the comma-separated argument list ( or no arguments)
);
}
This tester reports the failure of this test as follows: Error in the test number 103 new MTListTr().getFirst() The test should fail: we supplied a wrong message correct exception: class: java.lang.UnsupportedOperationException incorrect message: No rest in an empty data set tester.ErrorReport: Error trace: at samples.Examples.testExceptions(Examples.java:626) actual: "message produced: No rest in an empty data set" expected: "message expected: No first element in an empty data set" Note: If the test results in throwing an incorrect exception, the diagnostic message is more detailed. It includes the expected exception, the name of the method, and the object that invoked the method. This should help in identifying errors in setting up the test case, as well as in the error diagnosis. The error shown below was caused by misspelling the name of the method in the test case:
incorrect exception was thrown:
exception thrown: java.lang.NullPointerException
exception expected: java.lang.RuntimeException
with the message: Variable does not have a value
after invoking the method evalx
by an object in the class: BinOp
object value was:
((5 > 8) && t)
new BinOp:1(
this.op = &&
new AndOp:2()
this.rand1 = (5 > 8)
new BinOp:3(
this.op = >
new GreaterThan:4()
this.rand1 = 5
new Value:5(
this.data = 5)
this.rand2 = 8
new Value:6(
this.data = 8))
this.rand2 = t
new BoolVar:7(
this.identifier = "t"))
tester.ErrorReport: Error trace:
at Examples.testExpEval(Expression.java:439)
Code sourcesHere 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. | ||||||||||||||||||||||||||||
| last updated on Mon Apr 4 15:26:25 EDT 2011 | generated with DrRacket |