From 56521886ddd6523fdb144a507fc2036c9c6fd5b5 Mon Sep 17 00:00:00 2001 From: Sylwester Lachiewicz Date: Sun, 2 Nov 2025 23:15:14 +0000 Subject: [PATCH] JUnit Jupiter best practices Use this link to re-run the recipe: https://app.moderne.io/recipes/org.openrewrite.java.testing.junit.JupiterBestPractices?organizationId=YTA5ODhiOTYtNDI5OS00OGY3LTg0NjctNGZiNmI4OTY1ZjY3 Co-authored-by: Moderne --- .../apache/maven/wrapper/DownloaderTest.java | 2 +- .../wrapper/HashAlgorithmVerifierTest.java | 2 +- .../org/apache/maven/wrapper/InstallerTest.java | 12 ++++++------ .../apache/maven/wrapper/PathAssemblerTest.java | 17 ++++++++--------- .../maven/wrapper/SnapshotDistributionTest.java | 6 +++--- .../wrapper/SystemPropertiesHandlerTest.java | 11 +++++------ .../maven/wrapper/WrapperExecutorTest.java | 10 +++++----- 7 files changed, 29 insertions(+), 31 deletions(-) diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/DownloaderTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/DownloaderTest.java index 3b19ca2c..c8f5443a 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/DownloaderTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/DownloaderTest.java @@ -62,7 +62,7 @@ void setUp() throws Exception { } @Test - void testDownload() throws Exception { + void download() throws Exception { assertTrue(Files.notExists(downloadFile)); download.download(sourceRoot, downloadFile); assertTrue(Files.exists(downloadFile)); diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/HashAlgorithmVerifierTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/HashAlgorithmVerifierTest.java index 498ee563..af80734b 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/HashAlgorithmVerifierTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/HashAlgorithmVerifierTest.java @@ -31,7 +31,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; -public class HashAlgorithmVerifierTest { +class HashAlgorithmVerifierTest { @TempDir private File temporaryFolder; diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/InstallerTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/InstallerTest.java index 04731eaa..228fdfd2 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/InstallerTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/InstallerTest.java @@ -23,7 +23,6 @@ import java.io.IOException; import java.io.OutputStream; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.FileVisitResult; @@ -45,6 +44,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; import static org.mockito.Mockito.mock; @@ -145,7 +145,7 @@ public void testCreateDist() throws Exception { } @Test - void testCreateDistWithExistingDistribution() throws Exception { + void createDistWithExistingDistribution() throws Exception { createTestZip(zipDestination); Files.createDirectories(mavenHomeDir); @@ -165,7 +165,7 @@ void testCreateDistWithExistingDistribution() throws Exception { } @Test - void testCreateDistWithExistingDistAndZipAndAlwaysUnpackTrue() throws Exception { + void createDistWithExistingDistAndZipAndAlwaysUnpackTrue() throws Exception { createTestZip(zipDestination); Files.createDirectories(mavenHomeDir); @@ -186,7 +186,7 @@ void testCreateDistWithExistingDistAndZipAndAlwaysUnpackTrue() throws Exception } @Test - void testCreateDistWithExistingZipAndDistAndAlwaysDownloadTrue() throws Exception { + void createDistWithExistingZipAndDistAndAlwaysDownloadTrue() throws Exception { createTestZip(zipDestination); Path garbage = mavenHomeDir.resolve("garbage"); @@ -210,7 +210,7 @@ void testCreateDistWithExistingZipAndDistAndAlwaysDownloadTrue() throws Exceptio } @Test - void testZipSlip() throws URISyntaxException { + void zipSlip() throws Exception { URL resource = getClass().getClassLoader().getResource("zip-slip.zip"); Path zipSlip = Paths.get(resource.toURI()); when(localDistribution.getZipFile()).thenReturn(zipSlip); @@ -220,7 +220,7 @@ void testZipSlip() throws URISyntaxException { install.createDist(configuration); fail("Should fail as it contains a zip slip."); } catch (Exception ex) { - assertTrue(ex instanceof ZipException); + assertInstanceOf(ZipException.class, ex); } } diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java index dfba6c23..988f4c30 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/PathAssemblerTest.java @@ -30,7 +30,6 @@ import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.fail; @@ -58,7 +57,7 @@ void distributionDirWithMavenUserHomeBase() throws Exception { Path distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir(); assertThat(distributionDir.getFileName().toString(), matchesRegexp("[a-z0-9]+")); - assertThat(distributionDir.getParent(), equalTo(file(TEST_MAVEN_USER_HOME + "/somePath/maven-0.9-bin"))); + assertEquals(distributionDir.getParent(), file(TEST_MAVEN_USER_HOME + "/somePath/maven-0.9-bin")); } @Test @@ -68,7 +67,7 @@ void distributionDirWithProjectBase() throws Exception { Path distributionDir = pathAssembler.getDistribution(configuration).getDistributionDir(); assertThat(distributionDir.getFileName().toString(), matchesRegexp("[a-z0-9]+")); - assertThat(distributionDir.getParent(), equalTo(file(currentDirPath() + "/somePath/maven-0.9-bin"))); + assertEquals(distributionDir.getParent(), file(currentDirPath() + "/somePath/maven-0.9-bin")); } @Test @@ -89,9 +88,9 @@ void distZipWithMavenUserHomeBase() throws Exception { configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip")); Path dist = pathAssembler.getDistribution(configuration).getZipFile(); - assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip")); + assertEquals("maven-1.0.zip", dist.getFileName().toString()); assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+")); - assertThat(dist.getParent().getParent(), equalTo(file(TEST_MAVEN_USER_HOME + "/somePath/maven-1.0"))); + assertEquals(dist.getParent().getParent(), file(TEST_MAVEN_USER_HOME + "/somePath/maven-1.0")); } @Test @@ -100,9 +99,9 @@ void distZipWithProjectBase() throws Exception { configuration.setDistribution(new URI("http://server/dist/maven-1.0.zip")); Path dist = pathAssembler.getDistribution(configuration).getZipFile(); - assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip")); + assertEquals("maven-1.0.zip", dist.getFileName().toString()); assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+")); - assertThat(dist.getParent().getParent(), equalTo(file(currentDirPath() + "/somePath/maven-1.0"))); + assertEquals(dist.getParent().getParent(), file(currentDirPath() + "/somePath/maven-1.0")); } @Test @@ -111,9 +110,9 @@ void distZipWithLocalWindowsPath() throws Exception { configuration.setDistribution(new URI("file:///C:/maven-1.0.zip")); Path dist = pathAssembler.getDistribution(configuration).getZipFile(); - assertThat(dist.getFileName().toString(), equalTo("maven-1.0.zip")); + assertEquals("maven-1.0.zip", dist.getFileName().toString()); assertThat(dist.getParent().getFileName().toString(), matchesRegexp("[a-z0-9]+")); - assertThat(dist.getParent().getParent(), equalTo(file(currentDirPath() + "/somePath/maven-1.0"))); + assertEquals(dist.getParent().getParent(), file(currentDirPath() + "/somePath/maven-1.0")); } private Path file(String path) { diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/SnapshotDistributionTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/SnapshotDistributionTest.java index 279d7262..72ce66e7 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/SnapshotDistributionTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/SnapshotDistributionTest.java @@ -53,7 +53,7 @@ void setUp() throws IOException { } @Test - void testSnapshotUrlParsing() throws Exception { + void snapshotUrlParsing() throws Exception { // Test that PathAssembler can handle snapshot URLs String snapshotUrl = "https://repository.apache.org/content/repositories/snapshots/org/apache/maven/apache-maven/4.1.0-SNAPSHOT/apache-maven-4.1.0-20250710.120440-1-bin.zip"; @@ -70,7 +70,7 @@ void testSnapshotUrlParsing() throws Exception { } @Test - void testWrapperExecutorWithSnapshotUrl() throws Exception { + void wrapperExecutorWithSnapshotUrl() throws Exception { // Create properties file with snapshot URL String snapshotUrl = "https://repository.apache.org/content/repositories/snapshots/org/apache/maven/apache-maven/4.1.0-SNAPSHOT/apache-maven-4.1.0-20250710.120440-1-bin.zip"; @@ -94,7 +94,7 @@ void testWrapperExecutorWithSnapshotUrl() throws Exception { } @Test - void testRegularReleaseUrlStillWorks() throws Exception { + void regularReleaseUrlStillWorks() throws Exception { // Ensure regular releases still work as before String releaseUrl = "https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip"; diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java index 9aa17fbf..10322459 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/SystemPropertiesHandlerTest.java @@ -28,10 +28,9 @@ import java.util.Properties; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; +import static org.junit.jupiter.api.Assertions.assertEquals; +import org.junit.jupiter.api.Test; class SystemPropertiesHandlerTest { @@ -43,7 +42,7 @@ void setupTempDir() throws IOException { } @Test - void testParsePropertiesFile() throws Exception { + void parsePropertiesFile() throws Exception { Path propFile = tmpDir.resolve("props"); Properties props = new Properties(); props.put("a", "b"); @@ -57,12 +56,12 @@ void testParsePropertiesFile() throws Exception { Map expected = new HashMap<>(); expected.put("c", "d"); - assertThat(SystemPropertiesHandler.getSystemProperties(propFile), equalTo(expected)); + assertEquals(SystemPropertiesHandler.getSystemProperties(propFile), expected); } @Test void ifNoPropertyFileExistShouldReturnEmptyMap() { Map expected = new HashMap<>(); - assertThat(SystemPropertiesHandler.getSystemProperties(tmpDir.resolve("unknown")), equalTo(expected)); + assertEquals(SystemPropertiesHandler.getSystemProperties(tmpDir.resolve("unknown")), expected); } } diff --git a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java index 5b388a00..a316a92b 100644 --- a/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java +++ b/maven-wrapper/src/test/java/org/apache/maven/wrapper/WrapperExecutorTest.java @@ -178,7 +178,7 @@ void failWhenPropertiesFileDoesNotExist() { } @Test - void testRelativeDistUrl() throws Exception { + void relativeDistUrl() throws Exception { properties = new Properties(); properties.put("distributionUrl", "some/relative/url/to/bin.zip"); @@ -191,7 +191,7 @@ void testRelativeDistUrl() throws Exception { } @Test - void testEnvironmentVariableOverwriteSimpleCase() throws Exception { + void environmentVariableOverwriteSimpleCase() throws Exception { final Map environmentVariables = new HashMap<>(); environmentVariables.put(MVNW_REPOURL, "https://repo/test"); @@ -206,7 +206,7 @@ void testEnvironmentVariableOverwriteSimpleCase() throws Exception { } @Test - void testEnvironmentVariableOverwriteMvnwRepoUrlTrailingSlash() throws Exception { + void environmentVariableOverwriteMvnwRepoUrlTrailingSlash() throws Exception { final Map environmentVariables = new HashMap<>(); environmentVariables.put(MVNW_REPOURL, "https://repo/test/"); properties = new Properties(); @@ -220,7 +220,7 @@ void testEnvironmentVariableOverwriteMvnwRepoUrlTrailingSlash() throws Exception } @Test - void testEnvironmentVariableOverwritePackageName() throws Exception { + void environmentVariableOverwritePackageName() throws Exception { final Map environmentVariables = new HashMap<>(); environmentVariables.put(MVNW_REPOURL, "https://repo/test"); properties = new Properties(); @@ -235,7 +235,7 @@ void testEnvironmentVariableOverwritePackageName() throws Exception { } @Test - void testEnvironmentVariableOverwritePackageNameTrailingSpace() throws Exception { + void environmentVariableOverwritePackageNameTrailingSpace() throws Exception { final Map environmentVariables = new HashMap<>(); environmentVariables.put(MVNW_REPOURL, "https://repo/test/"); properties = new Properties();