Skip to content

Commit

Permalink
Let Testable annotation target any element type
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sormuras committed Sep 9, 2020
1 parent 23e824f commit a245da0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
Original file line number Diff line number Diff line change
@@ -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";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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).
*
* <h3>{@code @Testable} may target any declaration element type</h3>
* <p>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
Expand Down

0 comments on commit a245da0

Please sign in to comment.