Search the web
Sign In
New User? Sign Up
junit · JUnit, the Java unit testing framework written by Kent Beck and Erich Gamma.
? Already a member? Sign in to Yahoo!

Yahoo! Groups Tips

Did you know...
Message search is now enhanced, find messages faster. Take it for a spin.

Best of Y! Groups

   Check them out and nominate your group.
Having problems with message search? Fill out this form to ensure your group is one of the first to be migrated to the new message search system.

Messages

  Messages Help
Advanced
how can make an exception as a true condition   Message List  
Reply | Forward Message #12263 of 22109 |
Re: [junit] Re: how can make an exception as a true condition

On Friday 05 November 2004 15:17, Alistair A. Israel wrote:
> > public void testSetWidthBadValue() {
> > MyRectangle rect = new MyRectangle();
> >
> > try{
> > rect.setWidth(-5);
> > fail("negative widths are illegal");
> > }
> > catch(IllegalArgumentException expected)
> > {}
> > }
>
> My comments to this approach (and it seems everybody else does this,
> as seen by three nearly identical responses to the original post):
>
> 1. Empty {} block. I dunno, I'm anal that way.

I have a pass() method in my extending-TestCase-superclass for just
this reason.

> 2. It's easy to make the mistake of catching Exception - which will
> cause the test to pass if _any_ exception is thrown, even if it wasn't
> the one expected.

I don't find it easy - I know what exception I want to catch, and
writing the "wrong" one gives me the heebie-jeebies.

--
Chris "electric hedgehog" Dollin




Fri Nov 5, 2004 3:33 pm

anover_alias
Offline Offline
Send Email Send Email

Forward
Message #12263 of 22109 |
Expand Messages Author Sort by Date

Hi all, I'm new in JUnit and want to ask a question. Suppose I have a MyRectangle class with this method : public void setWidth(int w) throws...
timotius_pamungkas
timotius_pam...
Offline Send Email
Nov 5, 2004
2:53 pm

... What I do is (and I think I got this from another mailing list): public void testIAEThrown() { MyRectangle rect = new MyRectangle(); boolean...
Alistair A. Israel
aisrael
Offline Send Email
Nov 5, 2004
3:06 pm

... public void testSetWidthBadValue() { MyRectangle rect = new MyRectangle(); try{ rect.setWidth(-5); fail("negative widths are illegal"); } ...
Bob Koss
koss@...
Send Email
Nov 5, 2004
3:06 pm

Hi Timotius, You can do it like this: public void testSetWidth() { MyRectangle rect = new MyRectangle(); Assert.assertNotNull(rect); try { rect.setWidth(-5); ...
John L. Webber
John.Webber@...
Send Email
Nov 5, 2004
3:07 pm

... Don't try. Write two tests. Write a test to ensure that correct width-setting works: public void testValidWidthSetting() { MyRectangle rect = new...
Chris Dollin
anover_alias
Offline Send Email
Nov 5, 2004
3:07 pm

... My comments to this approach (and it seems everybody else does this, as seen by three nearly identical responses to the original post): 1. Empty {} block....
Alistair A. Israel
aisrael
Offline Send Email
Nov 5, 2004
3:19 pm

... I have a pass() method in my extending-TestCase-superclass for just this reason. ... I don't find it easy - I know what exception I want to catch, and ...
Chris Dollin
anover_alias
Offline Send Email
Nov 5, 2004
3:34 pm

... Understood, but don't let being anal be an excuse to turn your brain off. Never trust absolute rules. ... If you see this: catch (Exception expected) {} ...
J. B. Rainsberger
nails762
Offline Send Email
Nov 5, 2004
9:27 pm

On Fri, 05 Nov 2004 16:27:19 -0500, J. B. Rainsberger ... Agreed. ... I realized that, too. I also realized it's because I tend to make further checks within...
Alistair Israel
aisrael
Offline Send Email
Nov 6, 2004
6:54 am

Well, thanks for your answers. It really helps......
timotius_pamungkas
timotius_pam...
Offline Send Email
Nov 8, 2004
3:39 pm

... http://c2.com/cgi/wiki?CodingJavaUnitExceptionTests If that URL is not exactly correct, Google this: site:c2.com JUnit exception -- J. B. (Joe) Rainsberger...
J. B. Rainsberger
nails762
Offline Send Email
Nov 5, 2004
9:24 pm

If the empty block bothers you (as it does me), make more assertions: catch (SomeException e) { assertTrue("offending field not included in error ...
Kevin Klinemeier
zipwow
Offline Send Email
Nov 6, 2004
2:07 am

... That's exactly what I do, something like: exceptionWasThrown = e.getMessage().equals("expected error message"); In hindsight, if you do that then yeah, it...
Alistair Israel
aisrael
Offline Send Email
Nov 6, 2004
6:47 am
Advanced

Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Guidelines - Help