harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mikhail Loenko" <mloe...@gmail.com>
Subject Re: should strings in exceptions match the reference implementation?
Date Mon, 17 Apr 2006 06:31:01 GMT
2006/4/17, Anton Avtamonov <anton.avtamonov@gmail.com>:
> 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!");
>                }
>            }


Why it is that complicated?

Why one have jump over utility methods in different classes or even folders to
understand what a 5-line test does?

testSomething() throw OtherException {
        try {
            ec.do_something_exceptional();
            fail("NullPointerException was not thrown!");
        } catch (NullPointerException e) {
            //expected
        }
}

Let's keep simple things simple

Thanks,
Mikhail

>            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
>
>

---------------------------------------------------------------------
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


Mime
View raw message