(I'm reasonably sure the answer is "no", but I want to make sure.)
In JUnit 5 you can write an extension that is an implementation of ParameterResolver
. Before your test runs, if the method in question has parameters, then an extension that implements ParameterResolver
can return the object suitable as an argument for that parameter.
You can also write an extension that is an implementation of InvocationInterceptor
, that is in charge of intercepting a test method's execution. You can get any arguments (such as those resolved by ParameterResolver
s), but it appears you cannot change them.
In terms of execution order, if there are relevant parameters, then a ParameterResolver
will "fire" first, and then any InvocationInterceptor
s will "fire" next.
(Lastly, if your test method declares parameters, but there are no ParameterResolver
s to resolve them, everything craps out.)
Putting this all together:
Consider the case when a parameter can't really be properly resolved until the stuff that an interceptor sets up prior to execution is complete:
What is the best way, if there is one, to have all of the following:
- A parameter that conceivably only the interceptor could resolve
- Deferred resolution of that parameter (i.e. the actual parameter value is not sought by the JUnit internals until interception time so that the interceptor could resolve it just-in-time before calling
proceed()
…?
(In my very concrete case, I got lucky: the parameter I'm interested in is an interface, so I "resolve" it to a dummy implementation, and then, at interception time, "fill" the dummy implementation with a delegate that does the real work. I can't think of a better way with the existing JUnit 5 toolkit.)
(I can almost get there if ReflectiveInvocationContext
would allow me to set its arguments: my resolveParameter
implementation could return null
and my interceptor could replace the null
reference it found in the arguments with an appropriate non-null
argument just-in-time.)
(I also am at least aware of the ExecutableInvoker
interface that is reachable from the ExtensionContext
, but I'm unclear how that would help me in this scenario, since parameter resolution happens before interception.)