@Tset // 컴파일 오류! 이런 애너테이션은 없어요
public void sum() { /* ... */ }
@Target(ElementType.METHOD) // 메서드에만 사용 가능해요
public @interface Test { }
@ExceptionTest(ArithmeticException.class)
public void divideByZero() { /* ... */ }
// 테스트 메서드임을 표시하는 애너테이션
@Retention(RetentionPolicy.RUNTIME) // 실행 중에도 유지됨
@Target(ElementType.METHOD) // 메서드에만 사용 가능
public @interface Test {
}
// 특정 예외가 발생해야 성공하는 테스트를 표시
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExceptionTest {
Class<? extends Throwable> value(); // 매개변수 (예외 클래스)
}
// 마커 애너테이션 사용
@Test
public static void sum() {
// 테스트 코드
}
// 매개변수가 있는 애너테이션 사용
@ExceptionTest(ArithmeticException.class)
public static void divideByZero() {
int result = 5 / 0; // ArithmeticException 발생
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExceptionTest {
Class<? extends Throwable>[] value(); // 배열 매개변수
}
// 사용 예시
@ExceptionTest({ArithmeticException.class, NullPointerException.class})
public static void testMultipleExceptions() {
// 이 중 하나의 예외가 발생하면 테스트 성공
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Repeatable(ExceptionTestContainer.class) // 반복 가능하게 만들기
public @interface ExceptionTest {
Class<? extends Throwable> value();
}
// 컨테이너 애너테이션
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExceptionTestContainer {
ExceptionTest[] value();
}
// 사용 예시
@ExceptionTest(ArithmeticException.class)
@ExceptionTest(NullPointerException.class)
public static void testMultipleExceptions() {
// 두 애너테이션을 따로 반복해서 사용
}