junit5 icon indicating copy to clipboard operation
junit5 copied to clipboard

Introduce @NullSource, @EmptySource, & @NullAndEmptySource for parameterized tests

Open ttddyy opened this issue 8 years ago • 18 comments

Overview

Similar to #1635, I stumbled upon a situation where I need to validate null in parameterized test.

@ParameterizedTest
@ValueSource(strings = { "wrong-input", "", " " })
void wrongInput(String input) {
  ...
}

It is certainly convenient to have a way of testing null use case without creating a custom provider or another test method just for null.

Original Proposal

How about adding a flag attribute to @ValueSource annotation such as testNull, checkNull, etc.

@ParameterizedTest
@ValueSource(strings = { "wrong-input", "", " " }, testNull=true)
void wrongInput(String input) {
  ...
}

Related Issues

  • Supersedes #1635

Deliverables

  • [x] Introduce @NullSource that provides null for any reference type and throws an exception for any primitive target type.
  • [x] Introduce @EmptySource that provides an empty object for supported types and throws an exception for unsupported types.
    • Initial supported types: String, List, Set, Map, and arrays of any type.
  • [x] Introduce @NullAndEmptySource composed annotation that combines @NullSource and @EmptySource.
  • [x] Document in User Guide.
  • [x] Document in Release Notes.

ttddyy avatar Oct 16 '18 20:10 ttddyy

We typically would not want to introduce an annotation attribute that is only applicable part of the time. For example, null does not make sense for a primitive target type.

On the other hand, you can already pass null arguments via @MethodSource or @CsvSource as follows.

@ParameterizedTest
@CsvSource({ "wrong-input,", ",", "' '," })
void test(String s) {
	System.out.println(s == null ? null : "'" + s + "'");
}

That's perhaps a bit of a hack, but... it works. 😇

sbrannen avatar Oct 16 '18 20:10 sbrannen

Oh yeah, the output for the above is:

'wrong-input'
null
' '

sbrannen avatar Oct 16 '18 20:10 sbrannen

You could define a @NullSource and use it in addition to @ValueSource.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(NullArgumentsProvider.class)
public @interface NullSource {}

class NullArgumentsProvider implements ArgumentsProvider {
	@Override
	public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
		return Stream.of(Arguments.of(new Object[context.getRequiredTestMethod().getParameterCount()]));
	}
}

marcphilipp avatar Oct 17 '18 05:10 marcphilipp

@junit-team/junit-lambda, Tentatively slated for 5.4 M1 for the purpose of team discussion in conjunction with #1635.

sbrannen avatar Oct 17 '18 09:10 sbrannen

Good point, @marcphilipp.

In light of that proposal and the discussions in #1635, if we decide to include any such built-in sources, I think it might be best to introduce the following.

  • @NullSource: provides null for any reference type and throws an exception for any primitive target type.
  • @EmptySource: provides an empty object for supported types and throws an exception for any primitive target type.
    • Initial supported types: String, List, Set, Map, and arrays of any type.

For the blank String source proposed in #1635, I think we should just let people continue to use @ValueSource( strings = { ... } ) for that purpose.

Thoughts?

sbrannen avatar Oct 17 '18 09:10 sbrannen

That would lead to solutions similar to the following for the proposals in #1635.

class Tests {

	@ParameterizedTest
	@NullSource
	@EmptySource
	void nullAndEmptyStrings(String str) {
		assertTrue(str == null || str.isEmpty());
	}

	@ParameterizedTest
	@NullSource
	@EmptySource
	void nullAndEmptyLists(List<?> list) {
		assertTrue(list == null || list.isEmpty());
	}
}

sbrannen avatar Oct 17 '18 10:10 sbrannen

Sounds great to have them in junit-jupiter-params

ttddyy avatar Oct 17 '18 16:10 ttddyy

And for blank strings, one can simply use @ValueSource in contrast to the proposal in #1635.

The following would supply null, empty, and blank strings as arguments.

class Tests {

	@ParameterizedTest
	@NullSource
	@EmptySource
	@ValueSource(strings = {" ", "   ", "\t", "\n"})
	void nullEmptyAndBlankStrings(String str) {
		assertTrue(str == null || str.trim().isEmpty());
	}
}

If somebody wants to reuse all three of those sources, it's straightforward to introduce a custom composed annotation that is meta-annotated with those three source annotations.

Of course, JUnit Jupiter could also provide a pre-built @NullAndEmptySource composed annotation out of the box if we want to.

sbrannen avatar Oct 18 '18 08:10 sbrannen

You could define a @NullSource and use it in addition to @ValueSource.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@ArgumentsSource(NullArgumentsProvider.class)
public @interface NullSource {}

class NullArgumentsProvider implements ArgumentsProvider {
	@Override
	public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
		return Stream.of(Arguments.of(new Object[context.getRequiredTestMethod().getParameterCount()]));
	}
}

I just noticed that your proposal would generate nulls for all arguments, which would be risky since that would prevent arguments being supplied by other ParameterResolvers.

sbrannen avatar Oct 18 '18 08:10 sbrannen

In light of my previous comment, I'm thinking that @NullSource and @EmptySource should only provide a single argument (namely, the first argument).

sbrannen avatar Oct 18 '18 08:10 sbrannen

Hi

If i understand right, those new annotations can be combined with another source annotation, to supplement it with the null or empty value?

That would solve the problem i tried to solve in #1635, so i like it.

As for a @NullAndEmptySource, i think it is one of the most common use case (if not the most common one). So it would be valuable to have it baseline in the framework.

jmaniquet avatar Oct 19 '18 19:10 jmaniquet

If i understand right, those new annotations can be combined with another source annotation, to supplement it with the null or empty value?

Yes, that is correct.

All sources are combined into a single stream:

https://github.com/junit-team/junit5/blob/45a8c8f5a695e2f8805b343680f7f208a8f9d4b4/junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java#L68-L93

That would solve the problem i tried to solve in #1635, so i like it.

Great. In light of that, I'll close #1635.

As for a @NullAndEmptySource, i think it is one of the most common use case (if not the most common one). So it would be valuable to have it baseline in the framework.

OK. We will take that into consideration.

Thanks for the feedback!

sbrannen avatar Oct 20 '18 10:10 sbrannen

Update: I have renamed this issue, reworked the description, and added Deliverables.

sbrannen avatar Oct 20 '18 10:10 sbrannen

In response to this blog which I discovered via this tweet, there is no need to implement a custom NullableConverter for use with @CsvSource.

Given the following DateRange:

import java.time.LocalDate;

public class DateRange {

	private final LocalDate startDate;
	private final LocalDate endDate;

	public DateRange(LocalDate startDate, LocalDate endDate) {
		if (startDate == null && endDate == null) {
			throw new IllegalArgumentException();
		}
		if (startDate != null && endDate != null && startDate.isAfter(endDate)) {
			throw new IllegalArgumentException();
		}
		this.startDate = startDate;
		this.endDate = endDate;
	}

	@Override
	public String toString() {
		return "DateRange [startDate=" + startDate + ", endDate=" + endDate + "]";
	}

}

... the solution to the problem mentioned in that blog is to use empty values as follows:

@ParameterizedTest
@CsvSource({ "2017-06-01, 2018-10-15", ", 2018-10-15", "2017-06-01," })
void shouldCreateValidDateRange(LocalDate startDate, LocalDate endDate) {
	System.out.println(new DateRange(startDate, endDate));
}

@ParameterizedTest
@CsvSource({ "2018-10-15, 2017-06-01", "," })
void shouldNotCreateInvalidDateRange(LocalDate startDate, LocalDate endDate) {
	assertThrows(IllegalArgumentException.class, () -> new DateRange(startDate, endDate));
}

sbrannen avatar Oct 27 '18 10:10 sbrannen

Team Decision:

Let's add all three annotations.

marcphilipp avatar Nov 23 '18 12:11 marcphilipp

in progress

sbrannen avatar Jan 05 '19 13:01 sbrannen

FYI: work on this issue can be viewed here:

https://github.com/junit-team/junit5/compare/issues/1637-null-and-empty-sources

sbrannen avatar Jan 05 '19 14:01 sbrannen

The work for this issue has been merged into master in c6f75a7537d57f3333d7c06a77cbdbf88547e90d.

sbrannen avatar Jan 06 '19 14:01 sbrannen