aboutsummaryrefslogtreecommitdiff
path: root/src/junit/extensions/ExceptionTestCase.java
blob: 70040856088cb13058147f901c268875fdce7c15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package junit.extensions;

import junit.framework.TestCase;

/**
 * A TestCase that expects an Exception of class fExpected to be thrown.
 * The other way to check that an expected exception is thrown is:
 * <pre>
 * try {
 *   shouldThrow();
 * }
 * catch (SpecialException e) {
 *   return;
 * }
 * fail("Expected SpecialException");
 * </pre>
 *
 * To use ExceptionTestCase, create a TestCase like:
 * <pre>
 * new ExceptionTestCase("testShouldThrow", SpecialException.class);
 * </pre>
 */
public class ExceptionTestCase extends TestCase {
	Class fExpected;

	public ExceptionTestCase(String name, Class exception) {
		super(name);
		fExpected= exception;
	}
	/**
	 * Execute the test method expecting that an Exception of
	 * class fExpected or one of its subclasses will be thrown
	 */
	protected void runTest() throws Throwable {
		try {
			super.runTest();
		}
		catch (Exception e) {
			if (fExpected.isAssignableFrom(e.getClass()))
				return;
			else
				throw e;
		}
		fail("Expected exception " + fExpected);
	}
}