Skip to content

Commit c9851a0

Browse files
committed
20 - Test Fixtures
1 parent 6ee7d67 commit c9851a0

File tree

22 files changed

+255
-22
lines changed

22 files changed

+255
-22
lines changed

.github/workflows/build.yaml

+27-21
Original file line numberDiff line numberDiff line change
@@ -14,126 +14,132 @@ jobs:
1414
- name: 01
1515
uses: gradle/[email protected]
1616
with:
17-
gradle-version: 7.2
17+
gradle-version: 7.5.1
1818
build-root-directory: 01_The_Settings_File/my-project
1919
arguments: :build
2020
- name: 02
2121
uses: gradle/[email protected]
2222
with:
23-
gradle-version: 7.2
23+
gradle-version: 7.5.1
2424
build-root-directory: 02_The_Build_Files/my-project
2525
arguments: :build
2626
- name: 03
2727
uses: gradle/[email protected]
2828
with:
29-
gradle-version: 7.2
29+
gradle-version: 7.5.1
3030
build-root-directory: 03_Plugins/my-project
3131
arguments: :build
3232
- name: 04
3333
uses: gradle/[email protected]
3434
with:
35-
gradle-version: 7.2
35+
gradle-version: 7.5.1
3636
build-root-directory: 04_Tasks/my-project
3737
arguments: :build
3838
- name: 05
3939
uses: gradle/[email protected]
4040
with:
41-
gradle-version: 7.2
41+
gradle-version: 7.5.1
4242
build-root-directory: 05_Lifecycle_Tasks/my-project
4343
arguments: :build
4444
- name: 06
4545
uses: gradle/[email protected]
4646
with:
47-
gradle-version: 7.2
47+
gradle-version: 7.5.1
4848
build-root-directory: 06_Configuring_Task_Inputs_And_Outputs/my-project
4949
arguments: :build
5050
- name: 07
5151
uses: gradle/[email protected]
5252
with:
53-
gradle-version: 7.2
53+
gradle-version: 7.5.1
5454
build-root-directory: 07_Implementing_Tasks_and_Extensions/my-project
5555
arguments: :build
5656
- name: 08
5757
uses: gradle/[email protected]
5858
with:
59-
gradle-version: 7.2
59+
gradle-version: 7.5.1
6060
build-root-directory: 08_Declaring_Dependencis/my-project
6161
arguments: :build
6262
- name: 09A
6363
uses: gradle/[email protected]
6464
with:
65-
gradle-version: 7.2
65+
gradle-version: 7.5.1
6666
build-root-directory: 09_Centralizing_Dependency_Versions/A_Dependency_Constrains_in_Convention_Plugin/my-project
6767
arguments: :build
6868
- name: 09B
6969
uses: gradle/[email protected]
7070
with:
71-
gradle-version: 7.2
71+
gradle-version: 7.5.1
7272
build-root-directory: 09_Centralizing_Dependency_Versions/B_Platform_Project/my-project
7373
arguments: :build
7474
- name: 09C
7575
uses: gradle/[email protected]
7676
with:
77-
gradle-version: 7.2
77+
gradle-version: 7.5.1
7878
build-root-directory: 09_Centralizing_Dependency_Versions/C_Version_Catalog/my-project
7979
arguments: :build
8080
- name: 09D
8181
uses: gradle/[email protected]
8282
with:
83-
gradle-version: 7.2
83+
gradle-version: 7.5.1
8484
build-root-directory: 09_Centralizing_Dependency_Versions/D_Dependency_Locking/my-project
8585
arguments: :build
8686
- name: 10
8787
uses: gradle/[email protected]
8888
with:
89-
gradle-version: 7.2
89+
gradle-version: 7.5.1
9090
build-root-directory: 10_Dependency_Version_Conflicts/my-project
9191
arguments: :build
9292
- name: 11
9393
uses: gradle/[email protected]
9494
with:
95-
gradle-version: 7.2
95+
gradle-version: 7.5.1
9696
build-root-directory: 11_Capability_Conflicts/my-project
9797
arguments: :build
9898
- name: 12
9999
uses: gradle/[email protected]
100100
with:
101-
gradle-version: 7.2
101+
gradle-version: 7.5.1
102102
build-root-directory: 12_Publishing_Libraries/my-project
103103
arguments: :build
104104
- name: 13
105105
uses: gradle/[email protected]
106106
with:
107-
gradle-version: 7.2
107+
gradle-version: 7.5.1
108108
build-root-directory: 13_Aggregating_Custom_Artifacts/my-project
109109
arguments: :build
110110
- name: 14
111111
uses: gradle/[email protected]
112112
with:
113-
gradle-version: 7.2
113+
gradle-version: 7.5.1
114114
build-root-directory: 14_Settings_Plugins/my-project
115115
arguments: :build
116116
- name: 16
117117
uses: gradle/[email protected]
118118
with:
119-
gradle-version: 7.2
119+
gradle-version: 7.5.1
120120
build-root-directory: 16_Source_Sets/my-project
121121
arguments: :build
122122
- name: 17
123123
uses: gradle/[email protected]
124124
with:
125-
gradle-version: 7.2
125+
gradle-version: 7.5.1
126126
build-root-directory: 17_Feature_Variants/my-project
127127
arguments: :build
128128
- name: 18
129129
uses: gradle/[email protected]
130130
with:
131-
gradle-version: 7.2
131+
gradle-version: 7.5.1
132132
build-root-directory: 18_Configuring_Testing/my-project
133133
arguments: :build
134134
- name: 19
135135
uses: gradle/[email protected]
136136
with:
137-
gradle-version: 7.2
137+
gradle-version: 7.5.1
138138
build-root-directory: 19_The_Test_Task/my-project
139+
arguments: :build
140+
- name: 20
141+
uses: gradle/[email protected]
142+
with:
143+
gradle-version: 7.5.1
144+
build-root-directory: 20_Test_Fixtures/my-project
139145
arguments: :build

20_Test_Fixtures/README.MD

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Understanding Gradle #20 – Test Fixtures
2+
3+
**👇 click thumbnail to watch video**
4+
5+
[<img src="https://onepiecesoftware.github.io/img/videos/20.png" width="640">](https://www.youtube.com/watch?v=fSRN6YKa5B0&list=PLWQK2ZdV4Yl2k2OmC_gsjDpdIBTN0qqkE)
6+
7+
How to share code among tests in different projects or builds?
8+
9+
## Explore this sample
10+
11+
This sample is best explored in [IntelliJ IDEA](https://www.jetbrains.com/idea/download).
12+
Open **`20_Test_Fixtures/my-project`** in IDEA and confirm with _Trust Project_.
13+
14+
## Gradle User Manual references
15+
16+
Each video only scratches the surfaces of the features shown.
17+
This is on purpose to give you a general understanding of how things work in Gradle.
18+
This understanding enables you to structure your project in a readable and maintainable way.
19+
20+
More details on each feature covered in this video can be found in the Gradle User Manual.
21+
The Manual, however, is very detailed on all kinds of topics and also contains information on legacy features (it's not always clear which these are).
22+
23+
To help you find the right places, here are some deep links into the Manual on the topics discussed in the video:
24+
25+
* [Test Fixtures](https://docs.gradle.org/current/userguide/java_testing.html#sec:java_test_fixtures)
26+
* [Feature Variants and Capabilities](https://docs.gradle.org/current/userguide/feature_variants.html)
27+
28+
## Need Gradle support?
29+
30+
Contact me, if you need help with Gradle: [onepiece.Software](http://onepiece.software).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
`kotlin-dsl`
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
plugins {
2+
id("my-java-base")
3+
id("application")
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
plugins {
2+
id("java")
3+
}
4+
5+
group = "org.example.my-app"
6+
version = "1.0"
7+
8+
java {
9+
toolchain.languageVersion.set(JavaLanguageVersion.of(11))
10+
}
11+
12+
tasks.test {
13+
useJUnitPlatform() // JUnit5
14+
}
15+
16+
dependencies {
17+
testImplementation("org.junit.jupiter:junit-jupiter-api")
18+
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
19+
}
20+
21+
// Centralized versions
22+
dependencies {
23+
testImplementation(platform("org.junit:junit-bom:5.8.2"))
24+
}
25+
dependencies.constraints {
26+
implementation("org.apache.commons:commons-lang3:3.9")
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
plugins {
2+
id("my-java-base")
3+
id("java-library")
4+
id("java-test-fixtures")
5+
}
6+
7+
val test = sourceSets.test.get()
8+
java.registerFeature(test.name) {
9+
usingSourceSet(test)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
dependencyResolutionManagement {
2+
repositories.gradlePluginPortal()
3+
}
4+
5+
include("structure-plugins")
6+
include("java-plugins")
7+
include("android-plugins")
8+
include("kotlin-plugins")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
`kotlin-dsl`
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Locations of Gradle plugins
2+
pluginManagement {
3+
repositories.gradlePluginPortal()
4+
}
5+
6+
// Location of other components
7+
dependencyResolutionManagement {
8+
repositories.mavenCentral()
9+
rootDir.parentFile.listFiles()?.filter {
10+
File(it, "settings.gradle.kts").exists()
11+
}?.forEach {
12+
includeBuild(it.path)
13+
}
14+
}
15+
16+
rootDir.listFiles()?.filter {
17+
File(it, "build.gradle.kts").exists()
18+
}?.forEach {
19+
include(it.name)
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
plugins {
2+
id("my-java-application")
3+
}
4+
5+
application {
6+
mainClass.set("myproject.MyApplication")
7+
}
8+
9+
dependencies {
10+
implementation("org.example.my-app:business-logic")
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"message":"Hello there :) (this was JSON)"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<MessageModel><message>Hello there :) (this was XML)</message></MessageModel>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package myproject;
2+
3+
import myproject.data.MessageModel;
4+
import myproject.services.Service;
5+
6+
public final class MyApplication {
7+
8+
public static void main(final String[] args) {
9+
Service.printMessage(new MessageModel("Hello there :)"));
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
plugins {
2+
id("my-java-library")
3+
}
4+
5+
dependencies {
6+
implementation("org.apache.commons:commons-lang3")
7+
8+
api("org.example.my-app:data-model") // alternative: project(":data-model")
9+
10+
testImplementation(testFixtures("org.example.my-app:data-model")) // short notation for ↓
11+
// testImplementation("org.example.my-app:data-model") {
12+
// capabilities { requireCapability("org.example.my-app:data-model-test-fixtures") }
13+
// }
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package myproject.services;
2+
3+
import myproject.data.MessageModel;
4+
import org.apache.commons.lang3.StringUtils;
5+
6+
public final class Service {
7+
8+
public static String printMessage(MessageModel message) {
9+
String trimmedMessage = StringUtils.trimToEmpty(message.getMessage());
10+
String encodedMessage = encode(trimmedMessage);
11+
System.out.println(encodedMessage);
12+
return encodedMessage;
13+
}
14+
15+
public static String encode(String s) {
16+
return s.replace(":)", "\uD83D\uDE03");
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package myproject.services;
2+
3+
import myproject.data.test.MessageModelFixture;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class ServiceTest {
9+
10+
@Test
11+
void emoji_in_message_is_encoded() {
12+
assertEquals(
13+
"Hi \uD83D\uDE03",
14+
Service.printMessage(MessageModelFixture.newMessage())
15+
);
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
plugins {
2+
id("my-java-library")
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package myproject.data;
2+
3+
public final class MessageModel {
4+
5+
private final String message;
6+
7+
public MessageModel(String message) {
8+
this.message = message;
9+
}
10+
11+
public String getMessage() {
12+
return message;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package myproject.data.test;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
7+
public class MessageModelTest {
8+
9+
@Test
10+
void model_contains_message_text_it_was_created_with() {
11+
assertEquals(
12+
"Hi :)",
13+
MessageModelFixture.newMessage().getMessage());
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package myproject.data.test;
2+
3+
import myproject.data.MessageModel;
4+
5+
public class MessageModelFixture {
6+
7+
public static MessageModel newMessage() {
8+
return new MessageModel("Hi :)");
9+
}
10+
}

0 commit comments

Comments
 (0)