#format java /* Sample of Recursion and JUnit */ import junit.framework.*; class FactorialException extends ArithmeticException { } class Factorial { private int fact(int aNum) { if(aNum < 1) throw new FactorialException(); if(aNum == 1) return 1; else return aNum * fact(aNum-1); } public int get(int aNum) { return fact(aNum); } } public class FactorialTest extends TestCase { public static void main(String[] args) { junit.textui.TestRunner.run(FactorialTest.class); } Factorial f = new Factorial(); public void test1() { assertEquals(1, f.get(1)); } public void test2() { assertEquals(2, f.get(2)); } public void testNegative() { int impossible; try { impossible = f.get(-2); fail("Factorial input must positive"); } catch (FactorialException success) { } } }