From: Tom Anderson on
On Tue, 18 May 2010, Rhino wrote:

>> public void testParseException() {
>> try {
>> myObj.getRGB("Bad value!");
>> fail("Bad value did not cause exception!");
>> } catch(IllegalArgumentException e) {
>> // Success!
>> }
>> }
>>
>> public void testGoodParse() {
>> assertEqual(white, myObj.getRGB("FFFFFF"));
>> }
>
> Thank you VERY much for the speedy AND accurate answer! It never
> occurred to me to try that! It solves my problem very simply and
> elegantly. :-)

Not as elegantly as my solution!

tom

--
The square-jawed homunculi of Tommy Hilfiger ads make every day an
existential holocaust. -- Scary Go Round
From: Lew on
Rhino wrote:

and failed to acknowledge Daniel Pitts, who said:
>>> public void testParseException() {
>>> try {
>>> myObj.getRGB("Bad value!");
>>> fail("Bad value did not cause exception!");
>>> } catch(IllegalArgumentException e) {
>>> // Success!
>>> }
>>> }
>>>
>>> public void testGoodParse() {
>>> assertEqual(white, myObj.getRGB("FFFFFF"));
>>> }

Rhino:
>> Thank you VERY much for the speedy AND accurate answer! It never
>> occurred to me to try that! It solves my problem very simply and
>> elegantly. :-)

Tom Anderson wrote:
> Not as elegantly as my solution!

Indeed. As someone who is just now getting used to JUnit 4 and who loves,
nay, adores annotations, I found tom's response exceedingly enlightening.

--
Lew
From: Patricia Shanahan on
Lew wrote:
> Rhino wrote:
>
> and failed to acknowledge Daniel Pitts, who said:
>>>> public void testParseException() {
>>>> try {
>>>> myObj.getRGB("Bad value!");
>>>> fail("Bad value did not cause exception!");
>>>> } catch(IllegalArgumentException e) {
>>>> // Success!
>>>> }
>>>> }
>>>>
>>>> public void testGoodParse() {
>>>> assertEqual(white, myObj.getRGB("FFFFFF"));
>>>> }
>
> Rhino:
>>> Thank you VERY much for the speedy AND accurate answer! It never
>>> occurred to me to try that! It solves my problem very simply and
>>> elegantly. :-)
>
> Tom Anderson wrote:
>> Not as elegantly as my solution!
>
> Indeed. As someone who is just now getting used to JUnit 4 and who
> loves, nay, adores annotations, I found tom's response exceedingly
> enlightening.
>

I have a slight problem with using the annotation approach alone. It can
only check for a throw of appropriate type. It cannot check other
features of the thrown object, such as its message or a wrapped
exception. As far as I can tell, a catch block is the only way to fully
test if the interface promises anything beyond a throw type.

Patricia
From: Daniel Pitts on
On 5/19/2010 5:35 AM, Tom Anderson wrote:
> On Tue, 18 May 2010, Rhino wrote:
>
>>> public void testParseException() {
>>> try {
>>> myObj.getRGB("Bad value!");
>>> fail("Bad value did not cause exception!");
>>> } catch(IllegalArgumentException e) {
>>> // Success!
>>> }
>>> }
>>>
>>> public void testGoodParse() {
>>> assertEqual(white, myObj.getRGB("FFFFFF"));
>>> }
>>
>> Thank you VERY much for the speedy AND accurate answer! It never
>> occurred to me to try that! It solves my problem very simply and
>> elegantly. :-)
>
> Not as elegantly as my solution!
True, I'm not used to using Annotations.

Although, the pattern I used can have additional benefits. It could be
that you want to test the state of the object after the exception:

public void testFoo() {
try {
foo.doSomething("broken");
fail();
} catch (BrokenException be) {
// Success
}
assertThat(foo, hasGoodState());
}

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
From: Arne Vajhøj on
On 19-05-2010 14:47, Patricia Shanahan wrote:
> Lew wrote:
>> Rhino wrote:
>> and failed to acknowledge Daniel Pitts, who said:
>>>>> public void testParseException() {
>>>>> try {
>>>>> myObj.getRGB("Bad value!");
>>>>> fail("Bad value did not cause exception!");
>>>>> } catch(IllegalArgumentException e) {
>>>>> // Success!
>>>>> }
>>>>> }
>>>>>
>>>>> public void testGoodParse() {
>>>>> assertEqual(white, myObj.getRGB("FFFFFF"));
>>>>> }
>>
>> Rhino:
>>>> Thank you VERY much for the speedy AND accurate answer! It never
>>>> occurred to me to try that! It solves my problem very simply and
>>>> elegantly. :-)
>>
>> Tom Anderson wrote:
>>> Not as elegantly as my solution!
>>
>> Indeed. As someone who is just now getting used to JUnit 4 and who
>> loves, nay, adores annotations, I found tom's response exceedingly
>> enlightening.
>
> I have a slight problem with using the annotation approach alone. It can
> only check for a throw of appropriate type. It cannot check other
> features of the thrown object, such as its message or a wrapped
> exception. As far as I can tell, a catch block is the only way to fully
> test if the interface promises anything beyond a throw type.

Checking the message can be necessary, but it is an indication that
some programmer were too reluctant to subclass.

Arne