Skip to content

Add List-based support to Arguments API #4574

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

mariokhoury4
Copy link

Overview

This PR adds List-based support to the Arguments API for parameterized tests.

New additions:

  • Arguments.of(List<@Nullable Object>)
  • Arguments.arguments(List<@Nullable Object>) (alias)
  • Arguments.argumentSet(String, List<@Nullable Object>)
  • Arguments.toList() — returns a mutable List<@Nullable Object>

These additions make it easier to dynamically build arguments from collections when using @ParameterizedTest.

All additions are tested in ArgumentsTests.java.

Fixes #4535


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

@mariokhoury4 mariokhoury4 force-pushed the arguments-list-support branch 4 times, most recently from 3a32cf8 to 9de99b7 Compare May 24, 2025 23:43
@mariokhoury4 mariokhoury4 force-pushed the arguments-list-support branch from 9de99b7 to 3f3aa29 Compare May 24, 2025 23:47
Comment on lines 189 to 192
static Arguments of(@Nullable List<@Nullable Object> arguments) {
if (arguments == null) {
return of((Object) null); // Properly wrap null
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static Arguments of(@Nullable List<@Nullable Object> arguments) {
if (arguments == null) {
return of((Object) null); // Properly wrap null
}
static Arguments of(List<@Nullable Object> arguments) {
Preconditions.notNull(arguments, "arguments must not be null");

Comment on lines 193 to 197
if (arguments.isEmpty()) {
// Must still return empty arguments array
return of(new Object[0]);
}
return () -> arguments.toArray(new Object[0]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (arguments.isEmpty()) {
// Must still return empty arguments array
return of(new Object[0]);
}
return () -> arguments.toArray(new Object[0]);
return of(arguments.toArray());

static ArgumentSet argumentSet(String name, List<@Nullable Object> arguments) {
Preconditions.notBlank(name, "name must not be null or blank");
Preconditions.notNull(arguments, "arguments list must not be null");
return new ArgumentSet(name, arguments.toArray(new Object[0]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return new ArgumentSet(name, arguments.toArray(new Object[0]));
return new ArgumentSet(name, arguments.toArray());

return Stream.of(arguments(Collections.emptyList()));
return Stream.of(Arguments.of((Object) Collections.emptyList()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me wonder whether we should choose different names instead of overloading of(), arguments(), and argumentSet() to avoid such breakages. Maybe copyOf, argumentsOf, and argumentSetOf()?

@junit-team/junit-5 WDYT?

Copy link
Member

@sbrannen sbrannen May 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, we unfortunately overlooked the fact that a single List is also a single Object, so we definitely cannot go with this as-is.

Existing code must still compile without adding casts.

Though, in our defense, it is easy to overlook something like that when "coding in the browser". 😉

Maybe copyOf, argumentsOf, and argumentSetOf()?

I would rather go with something consistent, instead of having "copy" in only one of the names.

How about something like fromList(), argumentsFromList(), and argumentSetFromList()?

We could also drop the List part of the names, if we are somehow sure we will never introduce yet-another-set-of-overloads. So, something like from(), argumentsFrom(), and argumentSetFrom().

Thoughts?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thoughtful feedback! I agree that it's important to avoid breaking existing usage. I also like the idea of going with a more general naming convention, so my preference would be to use from() instead of fromList() or argumentsFrom(). I'm happy to update the PR accordingly once there's consensus.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the shorter variants. We might even get away with adding overloads to that in the future because "conflicts" with List seem less likely than with Object...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have updated the code with the suggestions!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd pondered it more while away from the computer and decided that I'm in favor of the from(), argumentsFrom() and argumentSetFrom() variants because of the brevity and also because of the nice symmetry with the new toList() instance method.

from(List) and toList() work well together.

So, I'm glad to see you both agree and that you're already implemented the changes, @mariokhoury4. 👍

@@ -172,4 +176,72 @@ public String toString() {

}

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move these next to the other static factory methods above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion! I'll move the method next to the other static factory methods to keep things consistent.

@mariokhoury4 mariokhoury4 force-pushed the arguments-list-support branch from cbde5ed to a2ca294 Compare May 25, 2025 15:45
@mariokhoury4 mariokhoury4 force-pushed the arguments-list-support branch from a2ca294 to d566f95 Compare May 25, 2025 15:50
@@ -125,6 +129,77 @@ static ArgumentSet argumentSet(String name, @Nullable Object... arguments) {
return new ArgumentSet(name, arguments);
}

/**
* Factory method for creating an instance of {@code Arguments} based on
* the supplied {@code arguments} as a {@link List}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* the supplied {@code arguments} as a {@link List}.
* the supplied {@link List} of {@code arguments}.

(same below for other methods)

Comment on lines 136 to 137
* @param arguments the arguments as a List to be used for an invocation
* of the test method; must not be {@code null} but may contain {@code null}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param arguments the arguments as a List to be used for an invocation
* of the test method; must not be {@code null} but may contain {@code null}
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}

(same below for other methods)

* Convert the arguments to a new mutable {@link List} containing the same
* elements as {@link #get()}.
*
* <p>This is useful for test logic that benefits from {@code List} operations such as filtering,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please hard-wrap lines at 80 chars.

@@ -56,4 +60,50 @@ void argumentsReturnsSameArrayUsedForCreating() {
assertThat(arguments.get()).isSameAs(input);
}

@Test
void ofSupportsList() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void ofSupportsList() {
void fromSupportsList() {

}

@Test
void argumentsSupportsListAlias() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
void argumentsSupportsListAlias() {
void argumentsFromSupportsList() {


assertThat(result).containsExactly("a", 2, null); // preserves content
result.add("extra"); // confirms mutability
assertThat(result).contains("extra");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also verify that the array in arguments can't be modified.


@Test
void toListWorksOnEmptyArguments() {
Arguments arguments = Arguments.from(Arrays.asList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Arguments arguments = Arguments.from(Arrays.asList());
Arguments arguments = Arguments.of();


@Test
void toListReturnsMutableListOfArguments() {
Arguments arguments = Arguments.from(Arrays.asList("a", 2, null));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Arguments arguments = Arguments.from(Arrays.asList("a", 2, null));
Arguments arguments = Arguments.of("a", 2, null);

List<Object> input = Arrays.asList(1, "two", null, 3.0);
Arguments arguments = Arguments.from(input);

assertArrayEquals(new Object[] { 1, "two", null, 3.0 }, arguments.get());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add an assertion verifying that modifying the initial List has no effect on arguments

Comment on lines +136 to +139
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an instance of {@code Arguments}; never {@code null}.
* @since 6.0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an instance of {@code Arguments}; never {@code null}.
* @since 6.0
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}
* @return an instance of {@code Arguments}; never {@code null}
* @since 6.0

Comment on lines +156 to +158
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an instance of {@code Arguments}; never {@code null}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an instance of {@code Arguments}; never {@code null}.
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}
* @return an instance of {@code Arguments}; never {@code null}

Comment on lines +175 to +179
* @param name the name of the argument set; must not be {@code null}
* or blank.
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an {@code ArgumentSet}; never {@code null}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @param name the name of the argument set; must not be {@code null}
* or blank.
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an {@code ArgumentSet}; never {@code null}.
* @param name the name of the argument set; must not be {@code null}
* or blank
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}
* @return an {@code ArgumentSet}; never {@code null}

Comment on lines +197 to +198
* @return a mutable List of arguments; never {@code null} but may contain
* {@code null}.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @return a mutable List of arguments; never {@code null} but may contain
* {@code null}.
* @return a mutable List of arguments; never {@code null} but may contain
* {@code null}

Copy link
Contributor

@vlsi vlsi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering: have you considered Iterable<? extends @Nullable Object> rather than using a plain List?


Based on my experience with checkerframework's @Nullable, I would suggest List<? extends @Nullable Object> rather than List<Object>.

I've asked jspecify to clarify the documentation anyways: jspecify/jspecify#748

At the same time, it would be nice to add tests that pass something like List<String> to the newly added methods.

For instance, from(Files.readAllLines("path")). I guess the code won't compile if the method uses List<Object> signatures.

* @see #argumentsFrom(List)
*/
@API(status = EXPERIMENTAL, since = "6.0")
static Arguments from(List<@Nullable Object> arguments) {
Copy link
Contributor

@vlsi vlsi Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use generic variance?
Otherwise users would not be able to pass a list with non-nullable values.

Suggested change
static Arguments from(List<@Nullable Object> arguments) {
static Arguments from(List<?> arguments) {

* @see #argumentSet(String, Object...)
*/
@API(status = EXPERIMENTAL, since = "6.0")
static Arguments argumentsFrom(List<@Nullable Object> arguments) {
Copy link
Contributor

@vlsi vlsi Jun 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
static Arguments argumentsFrom(List<@Nullable Object> arguments) {
static Arguments argumentsFrom(List<?> arguments) {

@vlsi
Copy link
Contributor

vlsi commented Jun 5, 2025

One more thing: List<?> is a good enough replacement for List<? extends @Nullable Object>. In this case, the method should probably have a signature like from(Iterable<?>)

@marcphilipp
Copy link
Member

One more thing: List<?> is a good enough replacement for List<? extends @Nullable Object>

I didn't know that but sounds like you're right, according to the spec:

If an unbounded wildcard appears in a null-marked scope, then it has a single upper bound whose base type is Object and whose nullness operator is UNION_NULL.

Therefore, I think using List<?> as method parameter and return type of toList() makes sense.

Just wondering: have you considered Iterable rather than using a plain List?

The use case we're addressing here is constructing new Arguments instances from existing ones so I don't think that flexibility is needed. Moreover, we could introduce it later, if necessary.

Comment on lines +136 to +138
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an instance of {@code Arguments}; never {@code null}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is It really worth documenting never {@code null}.? I'm afraid it adds excessive verbosity.

I think the best practice is non-null by default, so WDYT of documenting only the places where nulls can appear rather than trying to document every possible case?

Suggested change
* @param arguments the arguments to be used for an invocation of the test
* method; must not be {@code null} but may contain {@code null}.
* @return an instance of {@code Arguments}; never {@code null}.
* @param arguments the arguments to be used for an invocation of the test
* method; it may contain {@code null} elements.
* @return an instance of {@code Arguments}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may be excessive but clear. Moreover, we'd have to change that throughout the entire code base.

@vlsi
Copy link
Contributor

vlsi commented Jun 5, 2025

@marcphilipp , sorry for asking the same thing twice, however, could you please clarify why do you mean Arguments.from(List), Arguments argumentsFrom(List), and ArgumentSet argumentSetFrom(String name, List) relate to "from existing ones"?

To me the second case of #4535 sounds like a natural case for using argumentsFrom(Iterable<?>) rather than a pure List.

  1. An Arguments can only be created from an array, not a collection.

I've no issues if Arguments.toList returns List. However, I believe on the consumption side, Arguments.from should accept Iterable so the users could supply various types of collections they have.

Of course, it would be great if Java had something like IterableWithPredefinedIterationOrder, so users don't accidentally pass HashSet and get their arguments shuffled.


If you mean you force List so the users do not supply collections like Stream...parallel()... and somehow complain "the order of arguments is wrong sometimes"? If that is the case, it is probably worth documenting as "this List<?> could have been Iterable<?>" is a typical code review case.

@marcphilipp
Copy link
Member

sorry for asking the same thing twice, however, could you please clarify why do you mean Arguments.from(List), Arguments argumentsFrom(List), and ArgumentSet argumentSetFrom(String name, List) relate to "from existing ones"?

The use case we're trying to address here is to go from an existing Arguments instance to a new one without re-inventing a new collection API.

I've no issues if Arguments.toList returns List. However, I believe on the consumption side, Arguments.from should accept Iterable so the users could supply various types of collections they have.

We'll discuss that in one of our next team calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce List support in the Arguments API for parameterized tests
4 participants