Skip to content

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

Closed as not planned
@pxzxj

Description

@pxzxj

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions