I have the following annotation to mark test code that should not be executed if a server does not listen on a host and port each specified in a properties file:
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(DisableOnServerNotListening.class)
public @interface DisabledOnServerNotListening
{
String propertyNameHost() default "host";
String propertyNamePort() default "port";
}
In a junit 5 ÈxecutionCondition I want to look up the actual configured host/port values from the property file to decide if the test should be run (port is listening) or not
(port is not listening). Because there may be more than one host/port configuration in my tests I need to be flexible regarding property names. So how can I access the values of the annotation? Debugging shows that there is a field called testDescriptor in ExtensionContext, but it is not exposed for access.
Looking at existing Jupiter functionality you can see that most existing implementations of
ExecutionConditionwork with annotations. Thus, what you want to do is possible.Let's look at (parts of) the implementation of
DisabledCondition:If you replace
DisabledwithDisabledOnServerNotListeningyou have a head start on the implementation of your ownExecutionConditionsubtype.The actual checking code for host availability would probably reside within the
toResultmethod. Similar to: