|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |
@Documented @Target(value=METHOD) @Retention(value=RUNTIME) @Inherited public @interface ExpectException
Used to annotate a test case method
that is expecting an exception to occur. If the exception or one of
its subclasses (if annotated with subclass = true
) occurs during execution of the test case, the test
case has a success
result.
If the exception doesn't occur or some other exception occurs, the test case has a
fail
result. If the exception is a compile-time checked exception, it will need to be
declared to be thrown on the test case method as per the language rules for the Java programming language. There is
no such requirement for unchecked exceptions.
Example:
@Fixture("Demonstration") class MyTestFixture { @Test @ExpectException(NullPointerException.class) public void myTestCase1() { } @Test @ExpectException(NullPointerException.class) public void myTestCase2() { throw new NullPointerException(); } @Test @ExpectException(NullPointerException.class) public void myTestCase3() { throw new IllegalArgumentException(); } @Test @ExpectException(value=RuntimeException.class, subclass=true) public void myTestCase4() { throw new NullPointerException(); } @Test @ExpectException(value=java.io.IOException.class, subclass=true) public void myTestCase5() throws java.io.FileNotFoundException { throw new java.io.FileNotFoundException(); } }In the example given above:
myTestCase1
will give a fail result because it declares to expect a
java.lang.NullPointerException, however it is never
thrown.myTestCase2
will give a success result because it declares to expect a
java.lang.NullPointerException and it is thrown in
the body of the method.myTestCase3
will give a fail result because it declares to expect a
java.lang.NullPointerException, however it is never
thrown and some other type of exception is thrown.myTestCase4
will give a success result because it declares to expect a
java.lang.RuntimeException or any of its subclasses,
and one of its subclasses,
java.lang.NullPointerException, is thrown in the
body of the method.myTestCase5
will give a success result because it declares to expect a
java.io.IOException or any of its subclasses,
and one of its subclasses,
java.io.FileNotFoundException, is thrown in the
body of the method. This method is compile-time checked and so must be declared on the body of the method as per the
language rules for the Java programming language.
Test
Required Element Summary | |
---|---|
Class<? extends Throwable> |
value
Returns the java.lang.Class that the test case is expecting will be thrown. |
Optional Element Summary | |
---|---|
boolean |
subclass
Returns true if the test case should succeed if a subclass of the exception is thrown,
false otherwise. |
Element Detail |
---|
public abstract Class<? extends Throwable> value
public abstract boolean subclass
true
if the test case should succeed if a subclass of the exception is thrown,
false
otherwise.
true
if the test case should succeed if a subclass of the exception is thrown,
false
otherwise.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: REQUIRED | OPTIONAL | DETAIL: ELEMENT |