From a245da0bd9132a71eda30e63fd32111e0f47c48f Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Sun, 23 Aug 2020 07:55:21 +0200 Subject: [PATCH] Let Testable annotation target any element type Prior to this commit the usage of `@Testable` was restricted to declarations of types, methods, and fields. As custom test engines may use different element types as their "testable constructs", this restriction is lifted in backward and future-compatible manner. By dropping the `@Target` annotation, the default behaviour as defined by the Java Language Specification applies: If an annotation of type java.lang.annotation.Target is not present on the declaration of an annotation type T, then T is applicable in all declaration contexts except type parameter declarations, and in no type contexts. https://docs.oracle.com/javase/specs/jls/se8/html/jls-9.html#jls-9.6.4.1 Closes #2391 --- .../release-notes/release-notes-5.7.0.adoc | 3 +- .../engine/TestableAnnotationTests.java | 48 +++++++++++++++++++ .../platform/commons/annotation/Testable.java | 8 ++-- 3 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestableAnnotationTests.java diff --git a/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0.adoc b/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0.adoc index 845904fa6713..e684f99ae608 100644 --- a/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0.adoc +++ b/documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0.adoc @@ -23,7 +23,8 @@ on GitHub. ==== New Features and Improvements -* ❓ +* The `@Testable` annotation may now target any element type, including fields, methods, + classes, packages, and modules. [[release-notes-5.7.0-junit-jupiter]] diff --git a/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestableAnnotationTests.java b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestableAnnotationTests.java new file mode 100644 index 000000000000..7d21c15f4e2c --- /dev/null +++ b/junit-jupiter-engine/src/test/java/org/junit/jupiter/engine/TestableAnnotationTests.java @@ -0,0 +1,48 @@ +/* + * Copyright 2015-2020 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package org.junit.jupiter.engine; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.junit.platform.commons.annotation.Testable; + +/** + * Integration test that verifies that the testable annotation may be + * attached to any element type. + * + * @since 5.7 + */ +class TestableAnnotationTests { + + @Test + void testAndRepeatedTest() { + assertNotNull(new TestableEverywhere().toString()); + } + + @Testable + static class TestableEverywhere { + + @Testable + final int field = 0; + + @Testable + TestableEverywhere() { + } + + @Testable + void test(@Testable int parameter) { + @Testable + var var = "var"; + } + } + +} diff --git a/junit-platform-commons/src/main/java/org/junit/platform/commons/annotation/Testable.java b/junit-platform-commons/src/main/java/org/junit/platform/commons/annotation/Testable.java index a6a4b2251b78..7305646c91b5 100644 --- a/junit-platform-commons/src/main/java/org/junit/platform/commons/annotation/Testable.java +++ b/junit-platform-commons/src/main/java/org/junit/platform/commons/annotation/Testable.java @@ -13,11 +13,9 @@ import static org.apiguardian.api.API.Status.STABLE; import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; import org.apiguardian.api.API; @@ -68,9 +66,13 @@ * is therefore required to discover tests based on information specific to * that test engine (e.g., annotations specific to that test engine). * + *

{@code @Testable} may target any declaration element type

+ *

Since JUnit Platform version 1.7, {@code @Testable} may target any + * declaration {@linkplain java.lang.annotation.ElementType element type}. + * That includes the aforementioned "method, field, or class" elements. + * * @since 1.0 */ -@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented