Use Named arguments in parameterized tests

This commit is contained in:
Sam Brannen
2022-03-16 14:45:47 +01:00
parent 8a0c4caff6
commit c462fe30ed
28 changed files with 416 additions and 366 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,9 @@ package org.springframework.mock.http.server.reactive;
import java.util.Arrays;
import java.util.stream.Stream;
import org.junit.jupiter.api.Assertions;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
@@ -31,6 +31,8 @@ import org.springframework.http.HttpMethod;
import org.springframework.web.util.UriComponentsBuilder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.junit.jupiter.api.Named.named;
/**
* Unit tests for {@link MockServerHttpRequest}.
@@ -63,21 +65,23 @@ class MockServerHttpRequestTests {
assertThat(request.getURI().toString()).isEqualTo("/foo%20bar?a=b&name%20A=value%20A1&name%20A=value%20A2&name%20B=value%20B1");
}
@ParameterizedTest
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource
void httpMethodNotNullOrEmpty(Executable executable) {
Exception ex = Assertions.assertThrows(IllegalArgumentException.class, executable);
assertThat(ex.getMessage()).contains("HTTP method is required.");
void httpMethodNotNullOrEmpty(ThrowingCallable callable) {
assertThatIllegalArgumentException()
.isThrownBy(callable)
.withMessageContaining("HTTP method is required.");
}
static Stream<Executable> httpMethodNotNullOrEmpty() {
static Stream<Named<ThrowingCallable>> httpMethodNotNullOrEmpty() {
String uriTemplate = "/foo bar?a=b";
return Stream.of(
() -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build(),
() -> MockServerHttpRequest.method((HttpMethod) null, uriTemplate).build(),
() -> MockServerHttpRequest.method((String) null, uriTemplate).build(),
() -> MockServerHttpRequest.method("", uriTemplate).build(),
() -> MockServerHttpRequest.method(" ", uriTemplate).build()
named("null HttpMethod, URI", () -> MockServerHttpRequest.method(null, UriComponentsBuilder.fromUriString(uriTemplate).build("")).build()),
named("null HttpMethod, uriTemplate", () -> MockServerHttpRequest.method((HttpMethod) null, uriTemplate).build()),
named("null String, uriTemplate", () -> MockServerHttpRequest.method((String) null, uriTemplate).build()),
named("empty String, uriTemplate", () -> MockServerHttpRequest.method("", uriTemplate).build()),
named("blank String, uriTemplate", () -> MockServerHttpRequest.method(" ", uriTemplate).build())
);
}
}

View File

@@ -40,6 +40,7 @@ import org.springframework.test.context.web.WebTestContextBootstrapper;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import static org.mockito.Mockito.mock;
import static org.springframework.test.context.BootstrapUtils.resolveTestContextBootstrapper;
@@ -104,9 +105,9 @@ class BootstrapUtilsTests {
/**
* @since 5.3
*/
@ParameterizedTest(name = "{0}")
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource
void resolveTestContextBootstrapperInEnclosingClassHierarchy(String name, Class<?> testClass, Class<?> expectedBootstrapper) {
void resolveTestContextBootstrapperInEnclosingClassHierarchy(Class<?> testClass, Class<?> expectedBootstrapper) {
assertBootstrapper(testClass, expectedBootstrapper);
}
@@ -129,7 +130,7 @@ class BootstrapUtilsTests {
}
private static Arguments args(Class<?> testClass, Class<? extends TestContextBootstrapper> expectedBootstrapper) {
return arguments(testClass.getSimpleName(), testClass, expectedBootstrapper);
return arguments(named(testClass.getSimpleName(), testClass), expectedBootstrapper);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,10 +27,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextLoader;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Named.named;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/**
@@ -53,13 +53,11 @@ class GenericXmlContextLoaderResourceLocationsTests {
@ParameterizedTest(name = "[{index}] {0}")
@MethodSource("contextConfigurationLocationsData")
void assertContextConfigurationLocations(String testClassName, String[] expectedLocations) throws Exception {
Class<?> testClass = ClassUtils.forName(getClass().getName() + "$" + testClassName, getClass().getClassLoader());
final ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
final ContextLoader contextLoader = new GenericXmlContextLoader();
final String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
final String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
void assertContextConfigurationLocations(Class<?> testClass, String[] expectedLocations) throws Exception {
ContextConfiguration contextConfig = testClass.getAnnotation(ContextConfiguration.class);
ContextLoader contextLoader = new GenericXmlContextLoader();
String[] configuredLocations = (String[]) AnnotationUtils.getValue(contextConfig);
String[] processedLocations = contextLoader.processLocations(testClass, configuredLocations);
if (logger.isDebugEnabled()) {
logger.debug("----------------------------------------------------------------------");
@@ -73,29 +71,32 @@ class GenericXmlContextLoaderResourceLocationsTests {
static Stream<Arguments> contextConfigurationLocationsData() {
return Stream.of(
arguments(ClasspathNonExistentDefaultLocationsTestCase.class.getSimpleName(), array()),
args(ClasspathNonExistentDefaultLocationsTestCase.class, array()),
arguments(ClasspathExistentDefaultLocationsTestCase.class.getSimpleName(), array(
args(ClasspathExistentDefaultLocationsTestCase.class, array(
"classpath:org/springframework/test/context/support/GenericXmlContextLoaderResourceLocationsTests$ClasspathExistentDefaultLocationsTestCase-context.xml")),
arguments(ImplicitClasspathLocationsTestCase.class.getSimpleName(),
args(ImplicitClasspathLocationsTestCase.class,
array("classpath:/org/springframework/test/context/support/context1.xml",
"classpath:/org/springframework/test/context/support/context2.xml")),
arguments(ExplicitClasspathLocationsTestCase.class.getSimpleName(), array("classpath:context.xml")),
args(ExplicitClasspathLocationsTestCase.class, array("classpath:context.xml")),
arguments(ExplicitFileLocationsTestCase.class.getSimpleName(),
array("file:/testing/directory/context.xml")),
args(ExplicitFileLocationsTestCase.class, array("file:/testing/directory/context.xml")),
arguments(ExplicitUrlLocationsTestCase.class.getSimpleName(), array("https://example.com/context.xml")),
args(ExplicitUrlLocationsTestCase.class, array("https://example.com/context.xml")),
arguments(ExplicitMixedPathTypesLocationsTestCase.class.getSimpleName(),
args(ExplicitMixedPathTypesLocationsTestCase.class,
array("classpath:/org/springframework/test/context/support/context1.xml", "classpath:context2.xml",
"classpath:/context3.xml", "file:/testing/directory/context.xml",
"https://example.com/context.xml"))
);
}
private static Arguments args(Class<?> testClass, String[] expectedLocations) {
return arguments(named(testClass.getSimpleName(), testClass), expectedLocations);
}
private static String[] array(String... elements) {
return elements;
}