In addition, I want to share what I do when testing exceptions.
I created the following basic abstract class:
protected abstract class ExceptionalCase {
private Class clazz;
private String msg;
public abstract void exceptionalAction() throws Exception;
public ExceptionalCase() {
}
public ExceptionalCase(String msg, Class clazz) {
this.msg = msg;
this.clazz = clazz;
}
public Class expectedExceptionClass() {
return clazz;
}
public String expectedExceptionMessage() {
return msg;
}
}
and the following utility test method:
protected void testExceptionalCase(final ExceptionalCase ec) {
try {
ec.exceptionalAction();
fail("Exceptional case was not detected!");
} catch (final Exception e) {
if (ec.expectedExceptionClass() != null) {
if
(!ec.expectedExceptionClass().isAssignableFrom(e.getClass())) {
fail("Exception of wrong type " + e.getClass() + "
is produced!");
}
}
if (ec.expectedExceptionMessage() != null) {
assertEquals("Wrong exception message",
ec.expectedExceptionMessage(),
e.getMessage());
}
}
}
Besides, I created several 'concrete' ExceptionalCases like:
protected abstract class NullPointerCase extends ExceptionalCase {
public Class expectedExceptionClass() {
return NullPointerException.class;
}
}
So, every time when I want to test that "NPE and only NPE" is produced
I do the following:
testExceptionalCase(new NullPointerCase() {
public void exceptionalAction() throws Exception {
//some action which should produce NPE
}
});
It doesn't care about the message.
If I need to test some particular message as well as type of exception
I should either override expectedExceptionMessage() or use an
appropriate constructor to create ExceptionalCase like:
testExceptionalCase(new ExceptionalCase("expected message",
NullPointerException.class) {
public void exceptionalAction() throws Exception {
//some action which should produce NPE
}
});
I believe that is convenient and Object-Oriented approach.
Wishes,
--
Anton Avtamonov,
Intel Middleware Products Division
On 4/17/06, Anton Avtamonov <anton.avtamonov@gmail.com> wrote:
> On 4/17/06, Mark Hindess <mark.hindess@googlemail.com> wrote:
> > So, the second issue, should we be checking for messages/descriptions
> > in exception
> > tests, even to match what Harmony throws? If we do then our api tests
> > wont pass on other implementations.
>
> Hi Mark,
> As I said already I don't see any huge reasons to check the messages
> (probably for not being empty only). However I suppose sometimes it
> could be necessary. I'm pretty sure we will face many cases when we
> need to deviate from RI (it was discussed already in other threads).
>
> What I propose is to use for tests some utility method like
> isHarmony(). It will act as a 'formal' indicator of all our
> deviations.
>
> For this particular situation is should be something like:
> if (TestUtilities.isHarmony()) {
> assertEquals("some message", e.getMessage())
> }
>
> What do you think?
>
> --
> Anton Avtamonov,
> Intel Middleware Products Division
>
---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org
|