Skip to content

Commit 68a760b

Browse files
committed
Remove default dependencies from test suite when folder does not exist
Resolves #40
1 parent 82407b4 commit 68a760b

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/main/java/org/gradlex/javamodule/testing/JavaModuleTestingExtension.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,20 @@ public JavaModuleTestingExtension(Project project) {
6262
if ("test".equals(jvmTestSuite.getName())) {
6363
jvmTestSuite.useJUnitJupiter(); // override old Gradle convention to default to JUnit5 for all suites
6464
}
65+
66+
boolean testFolderExists = jvmTestSuite.getSources().getJava().getSrcDirs().stream().anyMatch(File::exists);
6567
if (isTestModule) {
6668
blackbox(jvmTestSuite);
67-
} else {
68-
boolean testFolderExists = jvmTestSuite.getSources().getJava().getSrcDirs().stream().anyMatch(File::exists);
69-
if (testFolderExists) {
70-
whitebox(jvmTestSuite, conf -> conf.getOpensTo().add("org.junit.platform.commons"));
71-
}
69+
} else if (testFolderExists) {
70+
whitebox(jvmTestSuite, conf -> conf.getOpensTo().add("org.junit.platform.commons"));
71+
}
72+
73+
// Remove the dependencies added by Gradle in case the test directory is missing. This allows the use of 'useJUnitJupiter("")' without hassle.
74+
if (!testFolderExists) {
75+
project.getConfigurations().getByName(jvmTestSuite.getSources().getImplementationConfigurationName(), implementation ->
76+
implementation.withDependencies(dependencySet -> dependencySet.removeIf(d -> "org.junit.jupiter".equals(d.getGroup()) && "junit-jupiter".equals(d.getName()))));
77+
project.getConfigurations().getByName(jvmTestSuite.getSources().getRuntimeOnlyConfigurationName(), runtimeOnly ->
78+
runtimeOnly.withDependencies(dependencySet -> dependencySet.removeIf(d -> "org.junit.platform".equals(d.getGroup()) && "junit-platform-launcher".equals(d.getName()))));
7279
}
7380
});
7481
}

src/test/groovy/org/gradlex/javamodule/testing/test/CustomizationTest.groovy

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,20 @@ class CustomizationTest extends Specification {
101101
then:
102102
result.task(":app:test").outcome == TaskOutcome.SUCCESS
103103
}
104+
105+
def "build does not fail when JUnit has no version and the test folder is empty"() {
106+
given:
107+
appTestModuleInfoFile.parentFile.deleteDir()
108+
appBuildFile << '''
109+
testing.suites.withType<JvmTestSuite>().all {
110+
useJUnitJupiter("") // <- no version, we want to manage that ourselves
111+
}
112+
'''
113+
114+
when:
115+
def result = runTests()
116+
117+
then:
118+
result.task(":app:test").outcome == TaskOutcome.NO_SOURCE
119+
}
104120
}

0 commit comments

Comments
 (0)