There have been times where I’ve had the need to perform some custom code when a JUnit test failure occurs. A simple way to handle this would be to catch Throwable do you logic and then re-throw the exception.

Not the most elegant of solutions.. but works nonetheless. Heres an example using the selenium rc framework:

public void testNewSignInDEValidMember() throws Exception {

try {
selenium.open(“/tests/”);
selenium.click(“link=Member Fixture UI”);
selenium.waitForPageToLoad(“30000″);
selenium.select(“memberFixture:address:country”, “label=GERMANY”);
selenium.select(“memberFixture:member:currency”, “label=EUR”);
selenium.type(“memberFixture:address:city”, “Berlin”);
selenium.type(“memberFixture:address:provinceState”, “”);
selenium.click(“memberFixture:submit”);
selenium.waitForPageToLoad(“30000″);
String accountId = selenium.getText(“accountId”);
… etc…

} catch (Throwable ex) {
log.error(“Failure Occurred! ” + ex);
ex.printStackTrace();

// Do custom logic here… (ie: screencapture)

throw new Exception(ex);
}

Post to Twitter Tweet This Post