SysTest part II.: Exceptions
In this post I will try to describe exceptions handling using SysTest.
Very often applications throw exceptions and we have to be able to handle these situations correctly in our UnitTests. Let's say we have the following application class:
public class ThrowingApp
{
public static ThrowingApp construct()
{
;
return new ThrowingApp();
}
public void throwingErrorException()
{
;
throw Exception::Error;
}
}
To write a test for this method verifying that it really throws the exception we can write the following UnitTest:
void testThrowErrorException()
{
ThrowingApp theApp = ThrowingApp::construct();
;
try
{
theApp.throwingErrorException();
this.fail('Method throwErrorException didn\'t throw an exception.');
}
catch(Exception::Error)
{
this.info('An expected exception has been caught.');
}
}
This works but SysTest offers another way of handling exceptions. SysTestCase provides parmExceptionExpected property to specify that the test expects the calling code to throw an exception. To do so set parmExceptionExpected property to true.
void testThrowErrorException2()
{
ThrowingApp theApp = ThrowingApp::construct();
;
this.parmExceptionExpected(true);
theApp.throwingErrorException();
}
This test is doing the same thing as the previous one. The property can actually do even more. Let's add the following two methods to our ThrowingApp class.
public void throwingErrorMessage()
{
;
throw error('Error message threw');
}
public void postingErrorMessage()
{
;
error('Error message posted');
}
The paramater parmExceptionExpected has an additional optional parameter named _exceptionMessage where you can specify a message that is expected to be thrown:
void testThrowErrorMessage()
{
ThrowingApp theApp = ThrowingApp::construct();
;
this.parmExceptionExpected(true, 'Error message threw');
theApp.throwingErrorMessage();
}
The same is valid also for error messages that are just added to the Infolog (they don't have to be thrown).
void testPostErrorMessage()
{
ThrowingApp theApp = ThrowingApp::construct();
;
this.parmExceptionExpected(true, 'Error message posted');
theApp.postingErrorMessage();
}
If the class throws an exception but the exception/error message is different than expected then SysTest will fail the test and correctly report expected exception/error message:
void testPostErrorMessage2()
{
ThrowingApp theApp = ThrowingApp::construct();
;
this.parmExceptionExpected(true, 'Different error message');
theApp.postingErrorMessage();
}
When you execute this test (using SysTest toolbar for example: Tools > Development Tools > Unit Test > Show toolbar) you see the following error message:
Error: [myExceptionTest.testPostErrorMessage] Failure: An exception was expected to be thrown!
(Expected: Different error message)
You can download these samples: Part02: Exceptions.xpo
In the next post I will try to describe test suites and their different "flavors".
Comments
Anonymous
October 10, 2006
We already know how to create test suites . Today I would like to cover some additional reasons for creatingAnonymous
August 15, 2007
I found a strange behaviour on parmExceptionExpected. Once it is enabled, it cannot be disabled in the same method and all subsequent assert statement doesn't work. This is an example. The "assertEquals(1,2)" always runs successfully. void testAssertAfterException() {; this.parmExceptionExpected(true); this.assertEquals(10/0,0,'divided by zero'); this.parmExceptionExpected(false); this.assertEquals(1,2); }Anonymous
July 23, 2008
I've treid some exceptions and found the same strange behavior as William mentioned. Is there a reason for this behavior or a workaround to get the test working correctly? By the way: Thanks for the great Blog David