[Junitdoclet-users] exception handling patch
Brought to you by:
sgemkow
From: Nicola F. <nic...@va...> - 2003-04-05 18:19:43
|
hello JUnitDoclet-users, lovers & developers! I hacked around a bit on JUnitDoclet and added a feature I really missed: If a tested method throws an exception, tests should take advantage of this. I've seen this being done with JuB (http://jub.sourceforge.net/) and thought it was a really nice idea. Consider the following method: public void setPriceCode(int i) throws RuntimeException { if(i != CHILDRENS || i != REGULAR || i != NEW_RELEASE){ throw new RuntimeException("illegal price code!"); } else { priceCode = i; } } This patch makes JUnitDoclet generate automatically a test-method body like this: public void testGetTitle() throws Exception { // JUnitDoclet begin method getTitle //insert code testing basic functionality try { movie.getTitle(null); } catch (Throwable e) { fail("Failed with:" + e); } //insert code triggering RuntimeException try { movie.getTitle(null); fail("Should raise RuntimeException"); } catch (RuntimeException e) { } catch (Throwable e) { fail("Failed with:" + e); } //insert code triggering Exception try { movie.getTitle(null); fail("Should raise Exception"); } catch (Exception e) { } catch (Throwable e) { fail("Failed with:" + e); } // JUnitDoclet end method getTitle } Of course, you have to add some bits & pieces to make the tests compilable, but the main idea is here... BTW, the whole thing works very well with multiple exceptions, too. To apply the patch, get the source distribution, unzip the src.zip in it and then execute this: patch -p1 -u < JUnitDoclet_exeception_handling.patch hope anyone will find this useful... regards nicola |