[PRL] JUnit4 preview: annotations replace name conventions

Mitchell Wand wand at ccs.neu.edu
Thu Sep 15 07:34:34 EDT 2005


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

--Mitch

-------------- next part --------------
HTML attachment scrubbed and removed


More information about the PRL mailing list