Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot get executing test method in ParameterResolver of @MethodSource factory method #3323

Closed
1 task done
pxzxj opened this issue May 25, 2023 · 6 comments
Closed
1 task done

Comments

@pxzxj
Copy link

pxzxj commented May 25, 2023

Deliverables

  • Get test method in ParameterResolver of parameterizedTest factory method. for example below extensionContext.getTestMethod() should return method testWithFactoryMethodWithArguments
public class MyTest {

	@RegisterExtension
	static final IntegerResolver integerResolver = new IntegerResolver();

	@ParameterizedTest
	@MethodSource("factoryMethodWithArguments")
	void testWithFactoryMethodWithArguments(String argument) {
		assertTrue(argument.startsWith("2"));
	}

	static Stream<Arguments> factoryMethodWithArguments(int quantity) {
		return Stream.of(
				arguments(quantity + " apples"),
				arguments(quantity + " lemons")
		);
	}

	static class IntegerResolver implements ParameterResolver {

		@Override
		public boolean supportsParameter(ParameterContext parameterContext,
										 ExtensionContext extensionContext) {
			return parameterContext.getParameter().getType() == int.class;
		}

		@Override
		public Object resolveParameter(ParameterContext parameterContext,
									   ExtensionContext extensionContext) {
			Optional<Method> testMethod = extensionContext.getTestMethod();
			boolean present = testMethod.isPresent();  //false
			return 2;
		}

	}
}

Related issues

@sbrannen sbrannen changed the title Get executing test method in ParameterResolver of parameterizedTest factory method Canot get executing test method in ParameterResolver of @MethodSource factory method May 25, 2023
@marcphilipp
Copy link
Member

Accessing the method from the ParameterContext as mentioned below should work.

* <p>The {@link java.lang.reflect.Method} or {@link java.lang.reflect.Constructor}
* in which the parameter is declared can be retrieved via
* {@link ParameterContext#getDeclaringExecutable()}.

@marcphilipp marcphilipp closed this as not planned Won't fix, can't repro, duplicate, stale Jun 7, 2023
@pxzxj
Copy link
Author

pxzxj commented Jun 14, 2023

@marcphilipp actually parameterContext.getDeclaringExecutable() return the factory method, not test method

factory-method

@marcphilipp marcphilipp reopened this Jun 30, 2023
@marcphilipp marcphilipp changed the title Canot get executing test method in ParameterResolver of @MethodSource factory method Cannot get executing test method in ParameterResolver of @MethodSource factory method Jun 30, 2023
@marcphilipp
Copy link
Member

@pxzxj Thanks for the feedback! You're correct. This use case is not currently supported.

Team decision: Investigate whether it's feasible to support this use case.

@JojOatXGME
Copy link
Contributor

I took a short look at this issue while preparing #4032. I was not able to reproduce the issue descriped here on the current main branch (d013085). I copied the code from the description and executed it. For me, ExtensionContext.getTestMethod() did return the test method testWithFactoryMethodWithArguments in IntegerResolver.resolveParameter.

@juanplopes
Copy link

I've encountered this issue after having the same problem with version 5.10.0. Reading @JojOatXGME's comment, I took a chance and upgraded to 5.11.1 and, indeed, the problem does not happen on that version.

@marcphilipp
Copy link
Member

This was indeed fixed in #3905.

@JojOatXGME Thanks for trying to reproduce it! 👍

@marcphilipp marcphilipp closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants