org.junit.jupiter.params.ParameterizedTestParameterResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-jupiter-params Show documentation
Show all versions of junit-jupiter-params Show documentation
Module "junit-jupiter-params" of JUnit 5.
/*
* Copyright 2015-2021 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at
*
* https://www.eclipse.org/legal/epl-v20.html
*/
package org.junit.jupiter.params;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.junit.jupiter.api.Named;
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;
/**
* @since 5.0
*/
class ParameterizedTestParameterResolver implements ParameterResolver {
private final ParameterizedTestMethodContext methodContext;
private final Object[] arguments;
ParameterizedTestParameterResolver(ParameterizedTestMethodContext methodContext, Object[] arguments) {
this.methodContext = methodContext;
this.arguments = arguments;
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
Executable declaringExecutable = parameterContext.getDeclaringExecutable();
Method testMethod = extensionContext.getTestMethod().orElse(null);
int parameterIndex = parameterContext.getIndex();
// Not a @ParameterizedTest method?
if (!declaringExecutable.equals(testMethod)) {
return false;
}
// Current parameter is an aggregator?
if (this.methodContext.isAggregator(parameterIndex)) {
return true;
}
// Ensure that the current parameter is declared before aggregators.
// Otherwise, a different ParameterResolver should handle it.
if (this.methodContext.hasAggregator()) {
return parameterIndex < this.methodContext.indexOfFirstAggregator();
}
// Else fallback to behavior for parameterized test methods without aggregators.
return parameterIndex < this.arguments.length;
}
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
throws ParameterResolutionException {
return this.methodContext.resolve(parameterContext, extractPayloads(this.arguments));
}
@SuppressWarnings("unchecked")
private Object[] extractPayloads(Object[] arguments) {
return Arrays.stream(arguments) //
.map(argument -> {
if (argument instanceof Named) {
return ((Named