Skip to content

Commit e9aa551

Browse files
authored
Migrate existing Junit 4 usages to Junit 5 (#998)
1 parent 2c30155 commit e9aa551

33 files changed

+212
-596
lines changed

build.gradle.kts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ dependencies {
3333

3434
testImplementation("org.spockframework:spock-core:2.3-groovy-3.0") {
3535
exclude(group = "org.codehaus.groovy")
36+
exclude(group = "org.hamcrest")
3637
}
37-
testImplementation("org.spockframework:spock-junit4:2.3-groovy-3.0")
38-
testImplementation("xmlunit:xmlunit:1.6")
38+
testImplementation("org.xmlunit:xmlunit-legacy:2.10.0")
3939
testImplementation("org.apache.commons:commons-lang3:3.17.0")
4040
testImplementation("com.google.guava:guava:33.3.1-jre")
41-
testImplementation("org.junit.jupiter:junit-jupiter-api:5.11.3")
42-
testRuntimeOnly("org.junit.vintage:junit-vintage-engine:5.11.3")
43-
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
41+
testImplementation(platform("org.junit:junit-bom:5.11.3"))
42+
testImplementation("org.junit.jupiter:junit-jupiter")
43+
testImplementation("org.junit.platform:junit-platform-suite-engine")
4444
}
4545

4646
val isCI = providers.environmentVariable("CI").isPresent

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/caching/AbstractCachingSpec.groovy

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
44
import org.apache.commons.io.FileUtils
55
import org.gradle.testkit.runner.BuildResult
66
import org.gradle.testkit.runner.TaskOutcome
7-
import org.junit.Rule
8-
import org.junit.rules.TemporaryFolder
7+
import spock.lang.TempDir
8+
9+
import java.nio.file.Path
910

1011
import static org.gradle.testkit.runner.TaskOutcome.FROM_CACHE
1112
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
1213

1314
abstract class AbstractCachingSpec extends PluginSpecification {
14-
@Rule TemporaryFolder alternateDir
15+
@TempDir Path alternateDir
1516

1617
@Override
1718
def setup() {
@@ -41,7 +42,7 @@ abstract class AbstractCachingSpec extends PluginSpecification {
4142
List<String> cacheArguments = [ '--build-cache' ]
4243
cacheArguments.addAll(arguments)
4344
// TODO: Use PluginSpecification.run here to reuse flags, but cache tests failed for now, need to investigate.
44-
return runner.withProjectDir(alternateDir.root).withArguments(cacheArguments).build()
45+
return runner.withProjectDir(alternateDir.toFile()).withArguments(cacheArguments).build()
4546
}
4647

4748
private String escapedPath(File file) {
@@ -59,9 +60,9 @@ abstract class AbstractCachingSpec extends PluginSpecification {
5960
}
6061

6162
void copyToAlternateDir() {
62-
FileUtils.deleteDirectory(alternateDir.root)
63-
FileUtils.forceMkdir(alternateDir.root)
64-
FileUtils.copyDirectory(dir.root, alternateDir.root)
63+
FileUtils.deleteDirectory(alternateDir.toFile())
64+
FileUtils.forceMkdir(alternateDir.toFile())
65+
FileUtils.copyDirectory(dir.toFile(), alternateDir.toFile())
6566
}
6667

6768
void assertShadowJarIsCachedAndRelocatable() {

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/caching/MinimizationCachingSpec.groovy

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
package com.github.jengelman.gradle.plugins.shadow.caching
22

3-
import org.gradle.testkit.runner.BuildResult
4-
5-
import static org.gradle.testkit.runner.TaskOutcome.*
6-
73
class MinimizationCachingSpec extends AbstractCachingSpec {
84
File output
95
String shadowJarTask = ":server:shadowJar"

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/ManualCodeSnippetTests.groovy

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,36 @@ import com.github.jengelman.gradle.plugins.shadow.docs.executer.GradleBuildExecu
44
import com.github.jengelman.gradle.plugins.shadow.docs.executer.NoopExecuter
55
import com.github.jengelman.gradle.plugins.shadow.docs.extractor.ManualSnippetExtractor
66
import com.github.jengelman.gradle.plugins.shadow.docs.fixture.GroovyDslFixture
7-
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.CodeSnippetTestCase
8-
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.CodeSnippetTests
7+
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.TestCodeSnippet
98
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.executer.SnippetExecuter
109
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.GroovyScriptFixture
1110
import com.google.common.base.StandardSystemProperty
11+
import org.junit.jupiter.api.DynamicTest
12+
import org.junit.jupiter.api.TestFactory
13+
import org.junit.jupiter.api.io.TempDir
1214

13-
class ManualCodeSnippetTests extends CodeSnippetTestCase {
15+
import java.nio.file.Path
1416

17+
class ManualCodeSnippetTests {
1518
public static final LinkedHashMap<String, SnippetExecuter> FIXTURES = [
1619
"groovy": new GradleBuildExecuter("build.gradle", new GroovyDslFixture(), new GroovyDslFixture.ImportsExtractor()),
1720
"groovy no-plugins": new GradleBuildExecuter("build.gradle", new GroovyScriptFixture(), new GroovyDslFixture.ImportsExtractor()),
1821
"groovy no-run": new NoopExecuter()
1922
]
2023

21-
@Override
22-
protected void addTests(CodeSnippetTests tests) {
24+
@TestFactory
25+
List<DynamicTest> provideDynamicTests(@TempDir Path tempDir) {
2326
File cwd = new File(StandardSystemProperty.USER_DIR.value())
24-
2527
def content = new File(cwd, "src/docs")
28+
List<TestCodeSnippet> snippets = []
2629

2730
FIXTURES.each { selector, executer ->
28-
ManualSnippetExtractor.extract(content, selector, executer).each {
29-
tests.add(it)
31+
ManualSnippetExtractor.extract(tempDir, content, selector, executer).each {
32+
snippets.add(it)
3033
}
3134
}
35+
return snippets.collect {
36+
DynamicTest.dynamicTest(it.testName, it)
37+
}
3238
}
33-
34-
}
39+
}

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/executer/GradleBuildExecuter.groovy

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.execute
55
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.fixture.SnippetFixture
66
import com.github.jengelman.gradle.plugins.shadow.util.PluginSpecification
77
import org.gradle.testkit.runner.GradleRunner
8-
import org.junit.rules.TemporaryFolder
98

109
import java.util.function.Function
1110

@@ -34,19 +33,15 @@ class GradleBuildExecuter implements SnippetExecuter {
3433
}
3534

3635
@Override
37-
void execute(TestCodeSnippet snippet) throws Exception {
38-
TemporaryFolder tempDir = new TemporaryFolder()
39-
tempDir.create()
40-
File dir = tempDir.newFolder()
41-
42-
addSubProject(dir)
43-
File settings = new File(dir, "settings.gradle")
36+
void execute(File tempDir, TestCodeSnippet snippet) throws Exception {
37+
addSubProject(tempDir)
38+
File settings = new File(tempDir, "settings.gradle")
4439
settings.text = """
4540
rootProject.name = 'shadowTest'
4641
include 'api', 'main'
4742
"""
4843

49-
File mainDir = new File(dir, "main")
44+
File mainDir = new File(tempDir, "main")
5045
mainDir.mkdirs()
5146
File buildFile = new File(mainDir, buildFile)
5247

@@ -59,7 +54,7 @@ include 'api', 'main'
5954

6055
buildFile.text = replaceTokens(fullSnippet)
6156

62-
GradleRunner runner = GradleRunner.create().withProjectDir(dir).withPluginClasspath().forwardOutput()
57+
GradleRunner runner = GradleRunner.create().withProjectDir(tempDir).withPluginClasspath().forwardOutput()
6358

6459
runner.withArguments(":main:build", "-m").build()
6560

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/executer/NoopExecuter.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class NoopExecuter implements SnippetExecuter {
1212
}
1313

1414
@Override
15-
void execute(TestCodeSnippet snippet) throws Exception {
16-
15+
void execute(File tempDir, TestCodeSnippet snippet) throws Exception {
16+
// noop
1717
}
1818
}

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/extractor/ManualSnippetExtractor.groovy

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.execute
55
import com.github.jengelman.gradle.plugins.shadow.docs.internal.snippets.executer.SnippetExecuter
66
import groovy.ant.FileNameFinder
77

8+
import java.nio.file.Path
89
import java.util.regex.Pattern
910

1011
class ManualSnippetExtractor {
1112

12-
static List<TestCodeSnippet> extract(File root, String cssClass, SnippetExecuter executer) {
13+
static List<TestCodeSnippet> extract(Path tempDir, File root, String cssClass, SnippetExecuter executer) {
1314
List<TestCodeSnippet> snippets = []
1415

1516
def snippetBlockPattern = Pattern.compile(/(?ims)```$cssClass\n(.*?)\n```/)
1617
def filenames = new FileNameFinder().getFileNames(root.absolutePath, "**/*.md")
1718

1819
filenames.each { filename ->
1920
def file = new File(filename)
20-
addSnippets(snippets, file, snippetBlockPattern, executer)
21+
addSnippets(tempDir, snippets, file, snippetBlockPattern, executer)
2122
}
2223

2324
snippets
2425
}
2526

26-
private static void addSnippets(List<TestCodeSnippet> snippets, File file, Pattern snippetBlockPattern, SnippetExecuter executer) {
27+
private static void addSnippets(Path tempDir, List<TestCodeSnippet> snippets, File file, Pattern snippetBlockPattern, SnippetExecuter executer) {
2728
def source = file.text
2829
String testName = file.parentFile.name + "/" +file.name
2930
Map<Integer, String> snippetsByLine = findSnippetsByLine(source, snippetBlockPattern)
3031

3132
snippetsByLine.each { lineNumber, snippet ->
32-
snippets << createSnippet(testName, file, lineNumber, snippet, executer)
33+
snippets << createSnippet(tempDir, testName, file, lineNumber, snippet, executer)
3334
}
3435
}
3536

@@ -60,8 +61,8 @@ class ManualSnippetExtractor {
6061
tag.substring(tag.indexOf("\n") + 1, tag.lastIndexOf("\n"))
6162
}
6263

63-
private static TestCodeSnippet createSnippet(String sourceClassName, File sourceFile, int lineNumber, String snippet, SnippetExecuter executer) {
64-
new TestCodeSnippet(snippet, sourceClassName, sourceClassName + ":$lineNumber", executer, new ExceptionTransformer(sourceClassName, sourceFile.name, lineNumber))
64+
private static TestCodeSnippet createSnippet(Path tempDir, String sourceClassName, File sourceFile, int lineNumber, String snippet, SnippetExecuter executer) {
65+
new TestCodeSnippet(tempDir, snippet, sourceClassName + ":$lineNumber", executer, new ExceptionTransformer(sourceClassName, sourceFile.name, lineNumber))
6566
}
6667

6768
}

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/internal/snippets/CodeSnippetTestCase.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/internal/snippets/CodeSnippetTests.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/test/groovy/com/github/jengelman/gradle/plugins/shadow/docs/internal/snippets/DefaultCodeSnippetTests.groovy

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)