Hi Benedikt, 

Usually I write tests using @Test(expected=IndexOutOfBoundsException.class) or 
add a throws in the method signature, like [1].

[1] http://junit.sourceforge.net/doc/faq/faq.htm#tests_7


But I haven't checked what version of JUnit you are using or carefully read the 
code under test to understand the context. So sorry if it has no relation with 
your issue :)

Cheers,

Bruno P. Kinoshita
http://kinoshita.eti.br
http://tupilabs.com


>________________________________
> From: Benedikt Ritter <b...@systemoutprintln.de>
>To: dev@commons.apache.org 
>Sent: Sunday, 22 January 2012 3:11 PM
>Subject: [SANDBOX][BeanUtils2] How to get rid of try/catch-blocks for expected 
>exceptions in unit tests
> 
>Hi,
>
>while I was working on the unit tests, I had to write several try catch blocks 
>in order to get all the failure cases for one methods tested in one test 
>method. I think those could be wrapped into some class, lets call it 
>ExpectedFailure.
>ExpectedFailure is abstract and clients have to implement the abstract method 
>thisMustFail(). The method is called from ExpectedFailure's constructor and is 
>wrapped inside a try catch block. If no Excpetion is thrown during execution 
>of thisMustFail(), a new AssertionError will be thrown. So instead of:
>
>@Test
>public void multipleFailuresExpceted()
>    try {
>        // do something that should throw an exception
>    myObject.myMethod(null, someParameter);
>        fail("Exception was not thrown!")
>    }
>    catch (NullPointerException e)
>    {
>        // we are happy because we got the exception we expected!
>    }
>
>    try {
>        // do something that should throw an exception
>    myObject.myMethod(someParamater, null);
>        fail("Exception was not thrown!")
>    }
>    catch (NullPointerException e)
>    {
>        // we are happy because we got the exception we expected!
>    }
>
>    // etc we would now test the null, null case... omitted in this example
>}
>
>we can write the following:
>
>@Test
>public void multipleFailuresExpceted() {
>
>    new ExpectedFailure(NullPointerException.class) {
>
>        @Override
>        void thisMustFail(){
>            myObject.myMethod(null, someParameter);
>        }
>    }
>
>    new ExpectedFailure(NullPointerException.class) {
>
>        @Override
>        void thisMustFail(){
>            myObject.myMethod(someParameter, null);
>        }
>    }
>
>    // null, null case omitted in this example
>}
>
>As you can see there are not that many lines, we can save with 
>ExpectedFailure. But with code completion, you can really benefit from it. 
>Also, I think it is better to have a class for wrapping those try catch 
>blocks, instead of having all that in your test methods. ExpectedFailure helps 
>you to focus on testing instead of catching your expected exceptions the right 
>way.
>
>If you like the idea, you can browse my github repo: 
>http://github.com/britter/sandbox/tree/master/util-test
>ExpectedFailure is located under src/main. I also implemented a unit test 
>under src/test, if you want to see some more examples.
>I would like to contribute ExpectedFailure, if you think it is beneficial for 
>BeanUtils2.
>
>regards
>Benedikt
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
>For additional commands, e-mail: dev-h...@commons.apache.org
>
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@commons.apache.org
For additional commands, e-mail: dev-h...@commons.apache.org

Reply via email to