"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "documentation/src/test/java/example/ParameterizedTestDemo.java" between
junit5-r5.3.2.tar.gz and junit5-r5.4.0.tar.gz

About: JUnit 5 is a unit testing framework for the Java programming language.

ParameterizedTestDemo.java  (junit5-r5.3.2):ParameterizedTestDemo.java  (junit5-r5.4.0)
/* /*
* Copyright 2015-2018 the original author or authors. * Copyright 2015-2019 the original author or authors.
* *
* All rights reserved. This program and the accompanying materials are * All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v2.0 which * made available under the terms of the Eclipse Public License v2.0 which
* accompanies this distribution and is available at * accompanies this distribution and is available at
* *
* http://www.eclipse.org/legal/epl-v20.html * http://www.eclipse.org/legal/epl-v20.html
*/ */
package example; package example;
skipping to change at line 34 skipping to change at line 34
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import java.lang.annotation.Target;
import java.time.LocalDate; import java.time.LocalDate;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream; import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import example.Person.Gender; import example.domain.Person;
import example.domain.Person.Gender;
import example.util.StringUtils;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.TestInfo; import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.TestReporter; import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterContext;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.AggregateWith; import org.junit.jupiter.params.aggregator.AggregateWith;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor; import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.aggregator.ArgumentsAggregator; import org.junit.jupiter.params.aggregator.ArgumentsAggregator;
import org.junit.jupiter.params.converter.ConvertWith; import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.converter.JavaTimeConversionPattern; import org.junit.jupiter.params.converter.JavaTimeConversionPattern;
import org.junit.jupiter.params.converter.SimpleArgumentConverter; import org.junit.jupiter.params.converter.SimpleArgumentConverter;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider; import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource; import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.EmptySource;
import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
class ParameterizedTestDemo { class ParameterizedTestDemo {
private static boolean isPalindrome(String candidate) {
int length = candidate.length();
for (int i = 0; i < length / 2; i++) {
if (candidate.charAt(i) != candidate.charAt(length - (i +
1))) {
return false;
}
}
return true;
}
// tag::first_example[] // tag::first_example[]
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" }) @ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
void palindromes(String candidate) { void palindromes(String candidate) {
assertTrue(isPalindrome(candidate)); assertTrue(StringUtils.isPalindrome(candidate));
} }
// end::first_example[] // end::first_example[]
// tag::ValueSource_example[] // tag::ValueSource_example[]
@ParameterizedTest @ParameterizedTest
@ValueSource(ints = { 1, 2, 3 }) @ValueSource(ints = { 1, 2, 3 })
void testWithValueSource(int argument) { void testWithValueSource(int argument) {
assertTrue(argument > 0 && argument < 4); assertTrue(argument > 0 && argument < 4);
} }
// end::ValueSource_example[] // end::ValueSource_example[]
@Nested
class NullAndEmptySource_1 {
// tag::NullAndEmptySource_example1[]
@ParameterizedTest
@NullSource
@EmptySource
@ValueSource(strings = { " ", " ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
assertTrue(text == null || text.trim().isEmpty());
}
// end::NullAndEmptySource_example1[]
}
@Nested
class NullAndEmptySource_2 {
// tag::NullAndEmptySource_example2[]
@ParameterizedTest
@NullAndEmptySource
@ValueSource(strings = { " ", " ", "\t", "\n" })
void nullEmptyAndBlankStrings(String text) {
assertTrue(text == null || text.trim().isEmpty());
}
// end::NullAndEmptySource_example2[]
}
// tag::EnumSource_example[] // tag::EnumSource_example[]
@ParameterizedTest @ParameterizedTest
@EnumSource(TimeUnit.class) @EnumSource(TimeUnit.class)
void testWithEnumSource(TimeUnit timeUnit) { void testWithEnumSource(TimeUnit timeUnit) {
assertNotNull(timeUnit); assertNotNull(timeUnit);
} }
// end::EnumSource_example[] // end::EnumSource_example[]
// tag::EnumSource_include_example[] // tag::EnumSource_include_example[]
@ParameterizedTest @ParameterizedTest
skipping to change at line 125 skipping to change at line 148
void testWithEnumSourceRegex(TimeUnit timeUnit) { void testWithEnumSourceRegex(TimeUnit timeUnit) {
String name = timeUnit.name(); String name = timeUnit.name();
assertTrue(name.startsWith("M") || name.startsWith("N")); assertTrue(name.startsWith("M") || name.startsWith("N"));
assertTrue(name.endsWith("SECONDS")); assertTrue(name.endsWith("SECONDS"));
} }
// end::EnumSource_regex_example[] // end::EnumSource_regex_example[]
// tag::simple_MethodSource_example[] // tag::simple_MethodSource_example[]
@ParameterizedTest @ParameterizedTest
@MethodSource("stringProvider") @MethodSource("stringProvider")
void testWithSimpleMethodSource(String argument) { void testWithExplicitLocalMethodSource(String argument) {
assertNotNull(argument); assertNotNull(argument);
} }
static Stream<String> stringProvider() { static Stream<String> stringProvider() {
return Stream.of("foo", "bar"); return Stream.of("apple", "banana");
} }
// end::simple_MethodSource_example[] // end::simple_MethodSource_example[]
// tag::simple_MethodSource_without_value_example[] // tag::simple_MethodSource_without_value_example[]
@ParameterizedTest @ParameterizedTest
@MethodSource @MethodSource
void testWithSimpleMethodSourceHavingNoValue(String argument) { void testWithDefaultLocalMethodSource(String argument) {
assertNotNull(argument); assertNotNull(argument);
} }
static Stream<String> testWithSimpleMethodSourceHavingNoValue() { static Stream<String> testWithDefaultLocalMethodSource() {
return Stream.of("foo", "bar"); return Stream.of("apple", "banana");
} }
// end::simple_MethodSource_without_value_example[] // end::simple_MethodSource_without_value_example[]
// tag::primitive_MethodSource_example[] // tag::primitive_MethodSource_example[]
@ParameterizedTest @ParameterizedTest
@MethodSource("range") @MethodSource("range")
void testWithRangeMethodSource(int argument) { void testWithRangeMethodSource(int argument) {
assertNotEquals(9, argument); assertNotEquals(9, argument);
} }
static IntStream range() { static IntStream range() {
return IntStream.range(0, 20).skip(10); return IntStream.range(0, 20).skip(10);
} }
// end::primitive_MethodSource_example[] // end::primitive_MethodSource_example[]
// @formatter:off // @formatter:off
// tag::multi_arg_MethodSource_example[] // tag::multi_arg_MethodSource_example[]
@ParameterizedTest @ParameterizedTest
@MethodSource("stringIntAndListProvider") @MethodSource("stringIntAndListProvider")
void testWithMultiArgMethodSource(String str, int num, List<String> list) { void testWithMultiArgMethodSource(String str, int num, List<String> list) {
assertEquals(3, str.length()); assertEquals(5, str.length());
assertTrue(num >=1 && num <=2); assertTrue(num >=1 && num <=2);
assertEquals(2, list.size()); assertEquals(2, list.size());
} }
static Stream<Arguments> stringIntAndListProvider() { static Stream<Arguments> stringIntAndListProvider() {
return Stream.of( return Stream.of(
arguments("foo", 1, Arrays.asList("a", "b")), arguments("apple", 1, Arrays.asList("a", "b")),
arguments("bar", 2, Arrays.asList("x", "y")) arguments("lemon", 2, Arrays.asList("x", "y"))
); );
} }
// end::multi_arg_MethodSource_example[] // end::multi_arg_MethodSource_example[]
// @formatter:on // @formatter:on
// @formatter:off
// tag::CsvSource_example[] // tag::CsvSource_example[]
@ParameterizedTest @ParameterizedTest
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" }) @CsvSource({
void testWithCsvSource(String first, int second) { "apple, 1",
assertNotNull(first); "banana, 2",
assertNotEquals(0, second); "'lemon, lime', 0xF1"
})
void testWithCsvSource(String fruit, int rank) {
assertNotNull(fruit);
assertNotEquals(0, rank);
} }
// end::CsvSource_example[] // end::CsvSource_example[]
// @formatter:on
// tag::CsvFileSource_example[] // tag::CsvFileSource_example[]
@ParameterizedTest @ParameterizedTest
@CsvFileSource(resources = "/two-column.csv", numLinesToSkip = 1) @CsvFileSource(resources = "/two-column.csv", numLinesToSkip = 1)
void testWithCsvFileSource(String first, int second) { void testWithCsvFileSource(String country, int reference) {
assertNotNull(first); assertNotNull(country);
assertNotEquals(0, second); assertNotEquals(0, reference);
} }
// end::CsvFileSource_example[] // end::CsvFileSource_example[]
// tag::ArgumentsSource_example[] // tag::ArgumentsSource_example[]
@ParameterizedTest @ParameterizedTest
@ArgumentsSource(MyArgumentsProvider.class) @ArgumentsSource(MyArgumentsProvider.class)
void testWithArgumentsSource(String argument) { void testWithArgumentsSource(String argument) {
assertNotNull(argument); assertNotNull(argument);
} }
// end::ArgumentsSource_example[] // end::ArgumentsSource_example[]
static static
// tag::ArgumentsProvider_example[] // tag::ArgumentsProvider_example[]
public class MyArgumentsProvider implements ArgumentsProvider { public class MyArgumentsProvider implements ArgumentsProvider {
@Override @Override
public Stream<? extends Arguments> provideArguments(ExtensionCont ext context) { public Stream<? extends Arguments> provideArguments(ExtensionCont ext context) {
return Stream.of("foo", "bar").map(Arguments::of); return Stream.of("apple", "banana").map(Arguments::of);
} }
} }
// end::ArgumentsProvider_example[] // end::ArgumentsProvider_example[]
// tag::ParameterResolver_example[] // tag::ParameterResolver_example[]
@BeforeEach @BeforeEach
void beforeEach(TestInfo testInfo) { void beforeEach(TestInfo testInfo) {
// ... // ...
} }
@ParameterizedTest @ParameterizedTest
@ValueSource(strings = "foo") @ValueSource(strings = "apple")
void testWithRegularParameterResolver(String argument, TestReporter testR eporter) { void testWithRegularParameterResolver(String argument, TestReporter testR eporter) {
testReporter.publishEntry("argument", argument); testReporter.publishEntry("argument", argument);
} }
@AfterEach @AfterEach
void afterEach(TestInfo testInfo) { void afterEach(TestInfo testInfo) {
// ... // ...
} }
// end::ParameterResolver_example[] // end::ParameterResolver_example[]
skipping to change at line 376 skipping to change at line 405
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER) @Target(ElementType.PARAMETER)
@AggregateWith(PersonAggregator.class) @AggregateWith(PersonAggregator.class)
public @interface CsvToPerson { public @interface CsvToPerson {
} }
// end::ArgumentsAggregator_with_custom_annotation_example_CsvToPerson[] // end::ArgumentsAggregator_with_custom_annotation_example_CsvToPerson[]
// @formatter:on // @formatter:on
// tag::custom_display_names[] // tag::custom_display_names[]
@DisplayName("Display name of container") @DisplayName("Display name of container")
@ParameterizedTest(name = "{index} ==> first=''{0}'', second={1}") @ParameterizedTest(name = "{index} ==> fruit=''{0}'', rank={1}")
@CsvSource({ "foo, 1", "bar, 2", "'baz, qux', 3" }) @CsvSource({ "apple, 1", "banana, 2", "'lemon, lime', 3" })
void testWithCustomDisplayNames(String first, int second) { void testWithCustomDisplayNames(String fruit, int rank) {
} }
// end::custom_display_names[] // end::custom_display_names[]
} }
 End of changes. 21 change blocks. 
34 lines changed or deleted 62 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)