[PRL] JUnit4 preview: annotations replace name conventions

Matthias Felleisen matthias at ccs.neu.edu
Thu Sep 15 07:50:53 EDT 2005


On Sep 15, 2005, at 7:34 AM, Mitchell Wand wrote:

> http://www-128.ibm.com/developerworks/java/library/j-junit4.html? 
> ca=dgr-lnxw01JUnit4
>
> gives a preview of the spiffy new version of JUnit.
>
>
> Instead of using name conventions (eg all test methods must start with  
> the string "test"), it uses Java5 annotations to mark the methods.
>
> And it has an annotation for marking "expected exceptions".  Instead  
> of writing
> public void testDivisionByZero() {
>
>     try {
>         int n = 2 / 0;
>         fail("Divided by zero!");
>     }
>     catch (ArithmeticException success) {
>         assertNotNull(success.getMessage());
>     }
>
> }
>
> you can write:
>
> @Test(expected=ArithmeticException.class) public void divideByZero() {
>  int n = 2 / 0;
>  }
>
>  Alas, this was our favorite example of why Java needed macros,  
> although he does say:
>> However, you'll still need to use the old try-catch style if you want  
>> to test the exception's detail message or other properties.
>>

Which was our first example on macros :-( 



More information about the PRL mailing list