Skip to content

Commit a245da0

Browse files
committed
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
1 parent 23e824f commit a245da0

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

documentation/src/docs/asciidoc/release-notes/release-notes-5.7.0.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ on GitHub.
2323

2424
==== New Features and Improvements
2525

26-
* ❓
26+
* The `@Testable` annotation may now target any element type, including fields, methods,
27+
classes, packages, and modules.
2728

2829

2930
[[release-notes-5.7.0-junit-jupiter]]
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2015-2020 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package org.junit.jupiter.engine;
12+
13+
import static org.junit.jupiter.api.Assertions.assertNotNull;
14+
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.platform.commons.annotation.Testable;
17+
18+
/**
19+
* Integration test that verifies that the testable annotation may be
20+
* attached to any element type.
21+
*
22+
* @since 5.7
23+
*/
24+
class TestableAnnotationTests {
25+
26+
@Test
27+
void testAndRepeatedTest() {
28+
assertNotNull(new TestableEverywhere().toString());
29+
}
30+
31+
@Testable
32+
static class TestableEverywhere {
33+
34+
@Testable
35+
final int field = 0;
36+
37+
@Testable
38+
TestableEverywhere() {
39+
}
40+
41+
@Testable
42+
void test(@Testable int parameter) {
43+
@Testable
44+
var var = "var";
45+
}
46+
}
47+
48+
}

junit-platform-commons/src/main/java/org/junit/platform/commons/annotation/Testable.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,9 @@
1313
import static org.apiguardian.api.API.Status.STABLE;
1414

1515
import java.lang.annotation.Documented;
16-
import java.lang.annotation.ElementType;
1716
import java.lang.annotation.Inherited;
1817
import java.lang.annotation.Retention;
1918
import java.lang.annotation.RetentionPolicy;
20-
import java.lang.annotation.Target;
2119

2220
import org.apiguardian.api.API;
2321

@@ -68,9 +66,13 @@
6866
* is therefore required to discover tests based on information specific to
6967
* that test engine (e.g., annotations specific to that test engine).
7068
*
69+
* <h3>{@code @Testable} may target any declaration element type</h3>
70+
* <p>Since JUnit Platform version 1.7, {@code @Testable} may target any
71+
* declaration {@linkplain java.lang.annotation.ElementType element type}.
72+
* That includes the aforementioned "method, field, or class" elements.
73+
*
7174
* @since 1.0
7275
*/
73-
@Target({ ElementType.TYPE, ElementType.METHOD, ElementType.FIELD })
7476
@Retention(RetentionPolicy.RUNTIME)
7577
@Inherited
7678
@Documented

0 commit comments

Comments
 (0)