Move tests to JUnit 5 wherever possible

This commit is contained in:
Andy Wilkinson
2019-05-24 11:24:29 +01:00
parent 36f56d034a
commit b18fffaf14
1320 changed files with 13424 additions and 14185 deletions

View File

@@ -28,15 +28,11 @@ import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.DynamicType.Builder;
import net.bytebuddy.implementation.FixedValue;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.testsupport.BuildOutput;
@@ -52,41 +48,34 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(Parameterized.class)
public class DevToolsIntegrationTests {
@ClassRule
public static final TemporaryFolder temp = new TemporaryFolder();
@TempDir
static File temp;
private static final BuildOutput buildOutput = new BuildOutput(DevToolsIntegrationTests.class);
private LaunchedApplication launchedApplication;
private final File serverPortFile;
private final File serverPortFile = new File(buildOutput.getRootLocation(), "server.port");
private final ApplicationLauncher applicationLauncher;
@RegisterExtension
final JvmLauncher javaLauncher = new JvmLauncher();
@Rule
public JvmLauncher javaLauncher = new JvmLauncher();
public DevToolsIntegrationTests(ApplicationLauncher applicationLauncher) {
this.applicationLauncher = applicationLauncher;
this.serverPortFile = new File(DevToolsIntegrationTests.buildOutput.getRootLocation(), "server.port");
}
@Before
public void launchApplication() throws Exception {
private void launchApplication(ApplicationLauncher applicationLauncher) throws Exception {
this.serverPortFile.delete();
this.launchedApplication = this.applicationLauncher.launchApplication(this.javaLauncher, this.serverPortFile);
this.launchedApplication = applicationLauncher.launchApplication(this.javaLauncher, this.serverPortFile);
}
@After
@AfterEach
public void stopApplication() throws InterruptedException {
this.launchedApplication.stop();
}
@Test
public void addARequestMappingToAnExistingController() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void addARequestMappingToAnExistingController(ApplicationLauncher applicationLauncher) throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -98,8 +87,11 @@ public class DevToolsIntegrationTests {
assertThat(template.getForObject(urlBase + "/two", String.class)).isEqualTo("two");
}
@Test
public void removeARequestMappingFromAnExistingController() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void removeARequestMappingFromAnExistingController(ApplicationLauncher applicationLauncher)
throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -109,8 +101,10 @@ public class DevToolsIntegrationTests {
.isEqualTo(HttpStatus.NOT_FOUND);
}
@Test
public void createAController() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void createAController(ApplicationLauncher applicationLauncher) throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -123,8 +117,10 @@ public class DevToolsIntegrationTests {
}
@Test
public void createAControllerAndThenAddARequestMapping() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void createAControllerAndThenAddARequestMapping(ApplicationLauncher applicationLauncher) throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -139,8 +135,11 @@ public class DevToolsIntegrationTests {
assertThat(template.getForObject(urlBase + "/three", String.class)).isEqualTo("three");
}
@Test
public void createAControllerAndThenAddARequestMappingToAnExistingController() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void createAControllerAndThenAddARequestMappingToAnExistingController(
ApplicationLauncher applicationLauncher) throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -157,8 +156,10 @@ public class DevToolsIntegrationTests {
assertThat(template.getForObject(urlBase + "/three", String.class)).isEqualTo("three");
}
@Test
public void deleteAController() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void deleteAController(ApplicationLauncher applicationLauncher) throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -170,8 +171,10 @@ public class DevToolsIntegrationTests {
}
@Test
public void createAControllerAndThenDeleteIt() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void createAControllerAndThenDeleteIt(ApplicationLauncher applicationLauncher) throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -215,8 +218,7 @@ public class DevToolsIntegrationTests {
return new ControllerBuilder(name, this.launchedApplication.getClassesDirectory());
}
@Parameters(name = "{0}")
public static Object[] parameters() throws IOException {
static Object[] parameters() throws IOException {
Directories directories = new Directories(buildOutput, temp);
return new Object[] { new Object[] { new LocalApplicationLauncher(directories) },
new Object[] { new ExplodedRemoteApplicationLauncher(directories) },

View File

@@ -28,14 +28,11 @@ import net.bytebuddy.description.annotation.AnnotationDescription;
import net.bytebuddy.description.modifier.Visibility;
import net.bytebuddy.dynamic.DynamicType;
import net.bytebuddy.implementation.FixedValue;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.testsupport.BuildOutput;
@@ -51,42 +48,36 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Madhura Bhave
*/
@RunWith(Parameterized.class)
public class DevToolsWithLazyInitializationIntegrationTests {
@ClassRule
public static final TemporaryFolder temp = new TemporaryFolder();
@TempDir
static File temp;
private static final BuildOutput buildOutput = new BuildOutput(DevToolsIntegrationTests.class);
private LaunchedApplication launchedApplication;
private final File serverPortFile;
private final File serverPortFile = new File(buildOutput.getRootLocation(), "server.port");
private final ApplicationLauncher applicationLauncher;
@RegisterExtension
final JvmLauncher javaLauncher = new JvmLauncher();
@Rule
public JvmLauncher javaLauncher = new JvmLauncher();
public DevToolsWithLazyInitializationIntegrationTests(ApplicationLauncher applicationLauncher) {
this.applicationLauncher = applicationLauncher;
this.serverPortFile = new File(buildOutput.getRootLocation(), "server.port");
}
@Before
public void launchApplication() throws Exception {
private void launchApplication(ApplicationLauncher applicationLauncher) throws Exception {
this.serverPortFile.delete();
this.launchedApplication = this.applicationLauncher.launchApplication(this.javaLauncher, this.serverPortFile,
this.launchedApplication = applicationLauncher.launchApplication(this.javaLauncher, this.serverPortFile,
"--spring.main.lazy-initialization=true");
}
@After
@AfterEach
public void stopApplication() throws InterruptedException {
this.launchedApplication.stop();
}
@Test
public void addARequestMappingToAnExistingControllerWhenLazyInit() throws Exception {
@ParameterizedTest(name = "{0}")
@MethodSource("parameters")
public void addARequestMappingToAnExistingControllerWhenLazyInit(ApplicationLauncher applicationLauncher)
throws Exception {
launchApplication(applicationLauncher);
TestRestTemplate template = new TestRestTemplate();
String urlBase = "http://localhost:" + awaitServerPort();
assertThat(template.getForObject(urlBase + "/one", String.class)).isEqualTo("one");
@@ -125,8 +116,7 @@ public class DevToolsWithLazyInitializationIntegrationTests {
return new ControllerBuilder(name, this.launchedApplication.getClassesDirectory());
}
@Parameterized.Parameters(name = "{0}")
public static Object[] parameters() throws IOException {
static Object[] parameters() throws IOException {
Directories directories = new Directories(buildOutput, temp);
return new Object[] { new Object[] { new LocalApplicationLauncher(directories) },
new Object[] { new ExplodedRemoteApplicationLauncher(directories) },

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@@ -18,8 +18,6 @@ package org.springframework.boot.devtools.tests;
import java.io.File;
import org.junit.rules.TemporaryFolder;
import org.springframework.boot.testsupport.BuildOutput;
/**
@@ -31,9 +29,9 @@ class Directories {
private final BuildOutput buildOutput;
private final TemporaryFolder temp;
private final File temp;
Directories(BuildOutput buildOutput, TemporaryFolder temp) {
Directories(BuildOutput buildOutput, File temp) {
this.buildOutput = buildOutput;
this.temp = temp;
}
@@ -43,7 +41,7 @@ class Directories {
}
File getRemoteAppDirectory() {
return new File(this.temp.getRoot(), "remote");
return new File(this.temp, "remote");
}
File getDependenciesDirectory() {
@@ -51,7 +49,7 @@ class Directories {
}
File getAppDirectory() {
return new File(this.temp.getRoot(), "app");
return new File(this.temp, "app");
}
}

View File

@@ -23,20 +23,20 @@ import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.junit.jupiter.api.extension.BeforeTestExecutionCallback;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.util.StringUtils;
/**
* JUnit {@link TestRule} that launched a JVM and redirects its output to a test
* {@link Extension} that launches a JVM and redirects its output to a test
* method-specific location.
*
* @author Andy Wilkinson
*/
class JvmLauncher implements TestRule {
class JvmLauncher implements BeforeTestExecutionCallback {
private static final Pattern NON_ALPHABET_PATTERN = Pattern.compile("[^A-Za-z]+");
@@ -45,11 +45,10 @@ class JvmLauncher implements TestRule {
private File outputDirectory;
@Override
public Statement apply(Statement base, Description description) {
public void beforeTestExecution(ExtensionContext context) throws Exception {
this.outputDirectory = new File(this.buildOutput.getRootLocation(),
"output/" + NON_ALPHABET_PATTERN.matcher(description.getMethodName()).replaceAll(""));
"output/" + NON_ALPHABET_PATTERN.matcher(context.getRequiredTestMethod().getName()).replaceAll(""));
this.outputDirectory.mkdirs();
return base;
}
LaunchedJvm launch(String name, String classpath, String... args) throws IOException {