Skip to content

Commit 281baf7

Browse files
committed
33 - Classpath and Module Path in Testing
1 parent 667f3be commit 281baf7

File tree

14 files changed

+147
-4
lines changed

14 files changed

+147
-4
lines changed

32_Artifact_Transforms/README.MD

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Understanding Gradle #32 – Artifact Transforms (Java Modularity Part 6)
1+
# Understanding Gradle #32 – Artifact Transforms (Java Modularity Part 7)
22

33
**👇 click thumbnail to watch video**
44

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Understanding Gradle #33 – Classpath and Module Path in Testing (Java Modularity Part 8)
2+
3+
**👇 click thumbnail to watch video**
4+
5+
[<img src="https://onepiecesoftware.github.io/img/videos/33.png" width="640">](https://www.youtube.com/watch?v=6rFEDcP8Noc&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)
6+
7+
How does modularity in Java work (Part 8): How are classpath and module path concepts used in testing?
8+
9+
## Explore this sample
10+
11+
This sample is best explored in [IntelliJ IDEA](https://www.jetbrains.com/idea/download).
12+
Open **`33_Classpath_and_Module_Path_in_Testing/my-project`** in IDEA and confirm with _Trust Project_.
13+
14+
## Further readings
15+
16+
* [Testing Java Modules](https://docs.gradle.org/current/userguide/java_testing.html#sec:java_testing_modular)
17+
* [Source Sets added by the Java Plugin](https://docs.gradle.org/current/userguide/java_plugin.html#source_sets)
18+
* [Testing in Java & JVM projects](https://docs.gradle.org/current/userguide/java_testing.html)
19+
* [Configurations added by the Java Library Plugin](https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_configurations_graph)
20+
* [JVM Test Suite plugin](https://docs.gradle.org/current/userguide/jvm_test_suite_plugin.html)
21+
* [Java Module Testing plugin](https://github.com/gradlex-org/java-module-testing)
22+
23+
24+
## Need Gradle support?
25+
26+
Contact me, if you need help with Gradle: [onepiece.Software](https://onepiece.software).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
plugins {
2+
`kotlin-dsl`
3+
}
4+
5+
dependencies {
6+
implementation("io.fuchs.gradle.classpath-collision-detector:classpath-collision-detector:0.3")
7+
implementation("org.gradlex:java-ecosystem-capabilities:1.2")
8+
implementation("org.gradlex:java-module-testing:1.1")
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
plugins {
2+
id("my-java-base")
3+
id("application")
4+
}
5+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
id("java")
3+
id("java-test-fixtures")
4+
id("org.gradlex.java-module-testing")
5+
}
6+
7+
group = "org.example"
8+
9+
java {
10+
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
11+
}
12+
13+
javaModuleTesting.whitebox(testing.suites.getByName("test")) {
14+
requires.add("org.junit.jupiter.api")
15+
opensTo.add("org.junit.platform.commons")
16+
}
17+
18+
tasks.test {
19+
useJUnitPlatform() // JUnit5
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugins {
2+
id("my-java-base")
3+
id("java-library")
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dependencyResolutionManagement {
2+
repositories.gradlePluginPortal()
3+
}
4+
5+
include("java-plugins")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
plugins {
2+
id("my-java-application")
3+
}
4+
5+
application {
6+
mainModule.set("org.example.app")
7+
mainClass.set("org.example.app.App")
8+
}
9+
10+
dependencies {
11+
implementation("org.apache.commons:commons-lang3:3.6")
12+
13+
testImplementation("org.apache.commons:commons-text:1.5")
14+
testImplementation("org.junit.jupiter:junit-jupiter-api:5.9.3")
15+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module org.example.app {
2+
requires org.apache.commons.lang3;
3+
4+
exports org.example.app;
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.app;
2+
3+
public class App {
4+
5+
public static void main(String[] args) {
6+
printModuleName();
7+
}
8+
9+
protected static void printModuleName() {
10+
System.out.println("Production Module: " +
11+
App.class.getModule().getName());
12+
}
13+
}
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.example.app;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
7+
import static java.nio.charset.StandardCharsets.UTF_8;
8+
9+
class AppTest {
10+
11+
@Test
12+
void testAppModule() throws IOException {
13+
App.printModuleName();
14+
System.out.println("Test Module: " +
15+
AppTest.class.getModule().getName());
16+
System.out.println(
17+
new String(AppTest.class.getResourceAsStream("/test-data.txt")
18+
.readAllBytes(), UTF_8));
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
__ __ __ __ ___ ______ ____
2+
/ / / /___ ____/ /__ __________/ /_____ _____ ____/ (_)___ ____ _ / ____/________ _____/ / /__
3+
/ / / / __ \/ __ / _ \/ ___/ ___/ __/ __ `/ __ \/ __ / / __ \/ __ `/ / / __/ ___/ __ `/ __ / / _ \
4+
/ /_/ / / / / /_/ / __/ / (__ ) /_/ /_/ / / / / /_/ / / / / / /_/ / / /_/ / / / /_/ / /_/ / / __/
5+
\____/_/ /_/\__,_/\___/_/ /____/\__/\__,_/_/ /_/\__,_/_/_/ /_/\__, / \____/_/ \__,_/\__,_/_/\___/
6+
/____/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Locations of Gradle plugins
2+
pluginManagement {
3+
repositories.gradlePluginPortal()
4+
includeBuild("../my-build-logic")
5+
}
6+
7+
// Location of other components
8+
dependencyResolutionManagement {
9+
repositories.mavenCentral()
10+
}
11+
12+
include("app")

README.MD

+4-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ This gives you a general understanding of how things work in Gradle and enables
3333
- [Full Android Project Setup](https://github.com/jjohannes/gradle-project-setup-howto/tree/android)
3434
- [Full Java Module System Project Setup](https://github.com/jjohannes/gradle-project-setup-howto/tree/java_module_system)
3535

36-
## JVM Development Basics (Entry 16 - 22)
36+
## JVM Development Basics (Entries 16 - 22)
3737

3838
16. [Source Sets](16_Source_Sets)
3939
17. [Feature Variants](17_Feature_Variants)
@@ -43,13 +43,13 @@ This gives you a general understanding of how things work in Gradle and enables
4343
21. [Test and Code Coverage Reporting](21_Test_and_Code_Coverage_Reporting)
4444
22. [The JavaCompile Task](22_The_JavaCompile_Task)
4545

46-
## General Gradle Topics (Entry 23 - 25)
46+
## General Gradle Topics (Entries 23 - 25)
4747

4848
23. [Caching](23_Caching)
4949
24. [Kotlin DSL and Groovy DSL](24_Kotlin_DSL_and_Groovy_DSL)
5050
25. [Using Java to configure builds](25_Using_Java_to_configure_builds)
5151

52-
## Java Modularity (Entry 26 - ??)
52+
## Java Modularity (Entries 26 - 33)
5353

5454
26. [The Classpath](26_The_Classpath)
5555
27. [Multiple Compile Classpaths](27_Multiple_Compile_Classpaths)
@@ -58,6 +58,7 @@ This gives you a general understanding of how things work in Gradle and enables
5858
30. [Discover Security Vulnerabilities](30_Security_Vulnerabilities)
5959
31. [The Module Path](31_The_Module_Path)
6060
32. [Artifact Transforms](32_Artifact_Transforms)
61+
33. [Classpath and Module Path in Testing](33_Classpath_and_Module_Path_in_Testing)
6162

6263
## Need Gradle support?
6364

0 commit comments

Comments
 (0)