Skip to content

Let @Testable target any declaration element type #2391

Closed
@sormuras

Description

@sormuras

With #2168 being addressed in milestone 5.7 M1 by extending the set of allowed element types it is possible to also annotate field declarations in addition to classes and methods:

https://github.com/junit-team/junit5/blob/68bb2e3124da100ff00608c44fcd75dd3061f593/junit-platform-commons/src/main/java/org/junit/platform/commons/annotation/Testable.java#L73

Another use-case for @Testable are test modules:

@Testable
open module test.integration {
  requires test.base; // read helper module
  requires org.junit.jupiter; // read Jupiter API modules
}
module test.base {
  exports test.base; // contains helpers
}

With such an annotation attached to a module declaration, clients (like IDEs and build tools) may detect ahead of time if a module wants to be launched in a test run. Here, the test.integration module is annotated with @Testable which would yield a call like:

junit --select-module test.integration

In this example, the test.base module is not annotated with @Testable -- which would prevent a similar junit call for module test.base. If a client launched junit for test.base, no test containers nor tests would be found and executed:

Test run finished after 1 ms
[         0 containers found      ]
[         0 containers skipped    ]
[         0 containers started    ]
[         0 containers aborted    ]
[         0 containers successful ]
[         0 containers failed     ]
[         0 tests found           ]
[         0 tests skipped         ]
[         0 tests started         ]
[         0 tests aborted         ]
[         0 tests successful      ]
[         0 tests failed          ]

Especially, if --fail-if-no-tests option is used, this may lead to surprising results.

Possible Solution

A forward-compatible solution is dropping the @Target annotation from Testable's declaration. Due to...

If an @Target meta-annotation is not present on an annotation type T , then an annotation of type T may be written as a modifier for any declaration except a type parameter declaration.

Copied from Target's API documentation. See also: https://docs.oracle.com/javase/specs/jls/se14/html/jls-9.html#jls-9.6.4.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions