1
2
3
4 import junit.framework.*;
5
6 class FactorialException extends ArithmeticException {
7 }
8
9 class Factorial {
10 private int fact(int aNum) {
11 if(aNum < 1)
12 throw new FactorialException();
13 if(aNum == 1)
14 return 1;
15 else
16 return aNum * fact(aNum-1);
17 }
18 public int get(int aNum) {
19 return fact(aNum);
20 }
21 }
22
23 public class FactorialTest extends TestCase {
24 public static void main(String[] args) {
25 junit.textui.TestRunner.run(FactorialTest.class);
26 }
27 Factorial f = new Factorial();
28 public void test1() {
29 assertEquals(1, f.get(1));
30 }
31 public void test2() {
32 assertEquals(2, f.get(2));
33 }
34 public void testNegative() {
35 int impossible;
36 try {
37 impossible = f.get(-2);
38 fail("Factorial input must positive");
39 } catch (FactorialException success) {
40 }
41 }
42 }