Java Reference
In-Depth Information
// Code goes here
}
// We are not expecting any exception
@TestCase()
public static void testCase2(){
// Code goes here
}
}
The
testCase1()
method specifies, using the
@TestCase
annotation, that it will throw an
IOException
. The test
runner tool will make sure that when it invokes this method, the method does throw an
IOException
. Otherwise,
it will fail the test case. The
testCase2()
method does not specify that it will throw an exception. If it throws an
exception when the test is run, the tool should fail this test case.
Enum Type
An annotation can have elements of an enum type. Suppose you want to declare an annotation type called
Review
that
can describe the code review status of a program element. Let's assume that it has a status element and it can have
one of the four values:
PENDING
,
FAILED
,
PASSED
, and
PASSEDWITHCHANGES
. You can declare an enum as an annotation
type member. Listing 1-8 shows the code for a
Review
annotation type.
Listing 1-8.
An Annotation Type, Which Uses an enum Type Element
// Review.java
package com.jdojo.annotation;
public @interface Review {
ReviewStatus status() default ReviewStatus.PENDING;
String comments() default "";
// ReviewStatus enum is a member of the Review annotation type
public enum ReviewStatus {PENDING, FAILED, PASSED, PASSEDWITHCHANGES};
}
The
Review
annotation type declares a
ReviewStatus
enum type and the four review statuses are the elements of
the enum. It has two elements,
status
and
comments
. The type of status element is the enum type
ReviewStatus
. The
default value for the
status
element is
ReviewStatus.PENDING
. You have an empty string as the default value for the
comments
element.
Here are some of the instances of the
Review
annotation type. You will need to import the
com.jdojo.annotation.Review.ReviewStatus
enum in your program to use the simple name of the
ReviewStatus
enum type.
// Have default for status and comments. Maybe code is new
@Review()
// Leave status as Pending, but add some comments
@Review(comments="Have scheduled code review on June 3 2014")
// Fail the review with comments
@Review(status=ReviewStatus.FAILED, comments="Need to handle errors")