-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge @FieldSource feature into main
This set of commits introduces @FieldSource which provides access to values returned from fields of the class in which the annotation is declared or from static fields in external classes referenced by fully qualified field name. The feature is analogous to the existing @MethodSource support. Closes #2014
- Loading branch information
Showing
13 changed files
with
1,594 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
documentation/src/test/java/example/ExternalFieldSourceDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2015-2024 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 example; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.FieldSource; | ||
|
||
class ExternalFieldSourceDemo { | ||
|
||
// tag::external_field_FieldSource_example[] | ||
@ParameterizedTest | ||
@FieldSource("example.FruitUtils#tropicalFruits") | ||
void testWithExternalFieldSource(String tropicalFruit) { | ||
// test with tropicalFruit | ||
} | ||
// end::external_field_FieldSource_example[] | ||
} | ||
|
||
class FruitUtils { | ||
|
||
public static final List<String> tropicalFruits = Collections.unmodifiableList(Arrays.asList("pineapple", "kiwi")); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
junit-jupiter-params/src/main/java/org/junit/jupiter/params/provider/ArgumentsUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2015-2024 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.provider; | ||
|
||
import static org.junit.jupiter.params.provider.Arguments.arguments; | ||
|
||
import org.junit.platform.commons.util.ReflectionUtils; | ||
|
||
/** | ||
* Collection of utilities for working with {@link Arguments}. | ||
* | ||
* @since 5.11, when it was extracted from {@link MethodArgumentsProvider} | ||
*/ | ||
final class ArgumentsUtils { | ||
|
||
private ArgumentsUtils() { | ||
/* no-op */ | ||
} | ||
|
||
/** | ||
* Convert the supplied object into an {@link Arguments} instance. | ||
*/ | ||
static Arguments toArguments(Object item) { | ||
// Nothing to do except cast. | ||
if (item instanceof Arguments) { | ||
return (Arguments) item; | ||
} | ||
|
||
// Pass all multidimensional arrays "as is", in contrast to Object[]. | ||
// See https://github.com/junit-team/junit5/issues/1665 | ||
if (ReflectionUtils.isMultidimensionalArray(item)) { | ||
return arguments(item); | ||
} | ||
|
||
// Special treatment for one-dimensional reference arrays. | ||
// See https://github.com/junit-team/junit5/issues/1665 | ||
if (item instanceof Object[]) { | ||
return arguments((Object[]) item); | ||
} | ||
|
||
// Pass everything else "as is". | ||
return arguments(item); | ||
} | ||
|
||
} |
Oops, something went wrong.