How to access ExecutableInvoker with the current ExtensionRegistry? #3896
-
I was hoping that ExecutableInvoker (accessible from The creation of the With this constructor for It appears that the new extension registry generated from This results in a default set of resolvers in all cases, regardless of which extension interface I use to get the extension context. Is there a correct way to utilize |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
This works expected: import static org.junit.jupiter.api.Assertions.assertEquals;
import java.lang.reflect.Method;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.api.extension.ParameterResolutionException;
import org.junit.jupiter.api.extension.ParameterResolver;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.platform.commons.support.ReflectionSupport;
@ExtendWith(ExecutableInvokerDemo.LongParameterResolver.class)
public class ExecutableInvokerDemo {
@RegisterExtension
Extension indirectResolver = new IndirectParameterResolver(ReflectionSupport.findMethod(getClass(), "method", long.class));
@Test
void test(AtomicLong value) {
assertEquals(42, value.get());
}
AtomicLong method(long value) {
return new AtomicLong(value);
}
static class LongParameterResolver implements ParameterResolver {
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return long.class.equals(parameterContext.getParameter().getType());
}
@Override
public Long resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return 42L;
}
}
static class IndirectParameterResolver implements ParameterResolver {
private final Method method;
public IndirectParameterResolver(Optional<Method> method) {
this.method = method.orElseThrow(() -> new ParameterResolutionException("Failed to find method"));
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return method.getReturnType().equals(parameterContext.getParameter().getType());
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
return extensionContext.getExecutableInvoker().invoke(method, extensionContext.getRequiredTestInstance());
}
}
} Please post a concrete example of what doesn't work for you. |
Beta Was this translation helpful? Give feedback.
Will be fixed in #3905.