Skip to content

Commit 053d224

Browse files
committed
Refactor TestSplit to return a string to improve code flow and testing
1 parent 6eb52f6 commit 053d224

File tree

5 files changed

+32
-57
lines changed

5 files changed

+32
-57
lines changed

build.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@ dependencies {
5454

5555
testImplementation(libs.assertj)
5656
testImplementation(libs.equalsVerifier)
57-
testImplementation(libs.systemStubs.core)
58-
testImplementation(libs.systemStubs)
5957
}
6058

6159
tasks.test {

gradle/libs.versions.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ jcommander = "1.82"
77
jetbrains-annotations = "26.0.2"
88
junit = "5.11.4"
99
logback = "1.5.16"
10-
systemStubs = "2.1.7"
1110

1211
[libraries]
1312
assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }
@@ -19,8 +18,6 @@ jetbrains-annotations = { module = "org.jetbrains:annotations", version.ref = "j
1918
junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
2019
junit-platform-launcher = { module = "org.junit.platform:junit-platform-launcher" }
2120
logback-classic = { module = "ch.qos.logback:logback-classic", version.ref = "logback" }
22-
systemStubs-core = { module = "uk.org.webcompere:system-stubs-core", version.ref = "systemStubs" }
23-
systemStubs = { module = "uk.org.webcompere:system-stubs-jupiter", version.ref = "systemStubs" }
2421

2522
[plugins]
2623
shadow = { id = "com.gradleup.shadow", version = "8.3.5" }

src/main/java/de/donnerbart/split/TestSplit.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public TestSplit(
6565
this.exitCodeConsumer = exitCodeConsumer;
6666
}
6767

68-
public void run() throws Exception {
68+
public @NotNull String run() throws Exception {
6969
LOG.info("Split index {} (total: {})", splitIndex, splitTotal);
7070
LOG.info("Working directory: {}", workingDirectory);
7171
LOG.info("Glob: {}", glob);
@@ -149,11 +149,11 @@ public void run() throws Exception {
149149
}
150150
final var split = splits.get(splitIndex);
151151
LOG.info("This test split has {} tests ({})", split.tests().size(), formatTime(split.totalRecordedTime()));
152-
System.out.print(split.tests()
152+
return split.tests()
153153
.stream()
154154
.sorted(Comparator.reverseOrder())
155155
.map(TestCase::name)
156-
.collect(Collectors.joining(" ")));
156+
.collect(Collectors.joining(" "));
157157
}
158158

159159
private static @NotNull Set<Path> getPaths(

src/main/java/de/donnerbart/split/TestSplitMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void main(final @Nullable String @NotNull [] args) throws Exceptio
4242
workingDirectory,
4343
arguments.debug,
4444
System::exit);
45-
testSplit.run();
45+
System.out.print(testSplit.run());
4646
}
4747

4848
@VisibleForTesting

src/test/java/de/donnerbart/split/TestSplitTest.java

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
import org.jetbrains.annotations.NotNull;
44
import org.junit.jupiter.api.BeforeEach;
55
import org.junit.jupiter.api.Test;
6-
import org.junit.jupiter.api.extension.ExtendWith;
76
import org.junit.jupiter.api.io.TempDir;
8-
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
9-
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
10-
import uk.org.webcompere.systemstubs.stream.SystemOut;
117

128
import java.nio.file.Files;
139
import java.nio.file.Path;
1410
import java.nio.file.attribute.PosixFilePermission;
11+
import java.util.ArrayList;
1512
import java.util.Set;
1613
import java.util.concurrent.atomic.AtomicReference;
1714

@@ -20,14 +17,10 @@
2017
import static java.nio.file.attribute.PosixFilePermission.OWNER_WRITE;
2118
import static org.assertj.core.api.Assertions.assertThat;
2219

23-
@ExtendWith(SystemStubsExtension.class)
2420
class TestSplitTest {
2521

2622
private static final @NotNull Set<PosixFilePermission> PERMISSIONS = Set.of(OWNER_READ, OWNER_WRITE);
2723

28-
@SystemStub
29-
private @NotNull SystemOut systemOut;
30-
3124
@TempDir
3225
private @NotNull Path tmp;
3326

@@ -84,15 +77,15 @@ void run_withoutJUnit_withOneSplit() throws Exception {
8477
tmp,
8578
true,
8679
exitCode::set);
87-
split.run();
8880

89-
assertThat(systemOut.getLines()).singleElement().isEqualTo( //
81+
assertThat(split.run()).isEqualTo(
9082
"de.donnerbart.example.FastTest de.donnerbart.example.NoTimingOneTest de.donnerbart.example.NoTimingTwoTest de.donnerbart.example.SlowTest de.donnerbart.example.SlowestTest");
9183
assertThat(exitCode).hasNullValue();
9284
}
9385

9486
@Test
9587
void run_withoutJUnit_withTwoSplits() throws Exception {
88+
final var splits = new ArrayList<String>();
9689
for (int index = 0; index <= 1; index++) {
9790
final var split = new TestSplit(index,
9891
2,
@@ -102,19 +95,18 @@ void run_withoutJUnit_withTwoSplits() throws Exception {
10295
tmp,
10396
true,
10497
exitCode::set);
105-
split.run();
106-
// add a new line to separate the captured output
107-
System.out.println();
98+
splits.add(split.run());
10899
}
109100

110-
assertThat(systemOut.getLines()).hasSize(2).containsSequence( //
101+
assertThat(splits).hasSize(2).containsExactly( //
111102
"de.donnerbart.example.FastTest de.donnerbart.example.NoTimingTwoTest de.donnerbart.example.SlowestTest",
112103
"de.donnerbart.example.NoTimingOneTest de.donnerbart.example.SlowTest");
113104
assertThat(exitCode).hasNullValue();
114105
}
115106

116107
@Test
117108
void run_withoutJUnit_withThreeSplits() throws Exception {
109+
final var splits = new ArrayList<String>();
118110
for (int index = 0; index <= 2; index++) {
119111
final var split = new TestSplit(index,
120112
3,
@@ -124,12 +116,10 @@ void run_withoutJUnit_withThreeSplits() throws Exception {
124116
tmp,
125117
true,
126118
exitCode::set);
127-
split.run();
128-
// add a new line to separate the captured output
129-
System.out.println();
119+
splits.add(split.run());
130120
}
131121

132-
assertThat(systemOut.getLines()).hasSize(3).containsSequence( //
122+
assertThat(splits).hasSize(3).containsExactly( //
133123
"de.donnerbart.example.FastTest de.donnerbart.example.SlowTest",
134124
"de.donnerbart.example.NoTimingOneTest de.donnerbart.example.SlowestTest",
135125
"de.donnerbart.example.NoTimingTwoTest");
@@ -146,15 +136,15 @@ void run_withJUnit_withOneSplit() throws Exception {
146136
tmp,
147137
true,
148138
exitCode::set);
149-
split.run();
150139

151-
assertThat(systemOut.getLines()).singleElement().isEqualTo( //
140+
assertThat(split.run()).isEqualTo(
152141
"de.donnerbart.example.SlowestTest de.donnerbart.example.SlowTest de.donnerbart.example.FastTest de.donnerbart.example.NoTimingOneTest de.donnerbart.example.NoTimingTwoTest");
153142
assertThat(exitCode).hasNullValue();
154143
}
155144

156145
@Test
157146
void run_withJUnit_withTwoSplits() throws Exception {
147+
final var splits = new ArrayList<String>();
158148
for (int index = 0; index <= 1; index++) {
159149
final var split = new TestSplit(index,
160150
2,
@@ -164,19 +154,18 @@ void run_withJUnit_withTwoSplits() throws Exception {
164154
tmp,
165155
true,
166156
exitCode::set);
167-
split.run();
168-
// add a new line to separate the captured output
169-
System.out.println();
157+
splits.add(split.run());
170158
}
171159

172-
assertThat(systemOut.getLines()).hasSize(2).containsSequence( //
160+
assertThat(splits).hasSize(2).containsExactly( //
173161
"de.donnerbart.example.SlowestTest",
174162
"de.donnerbart.example.SlowTest de.donnerbart.example.FastTest de.donnerbart.example.NoTimingOneTest de.donnerbart.example.NoTimingTwoTest");
175163
assertThat(exitCode).hasNullValue();
176164
}
177165

178166
@Test
179167
void run_withJUnit_withThreeSplits() throws Exception {
168+
final var splits = new ArrayList<String>();
180169
for (int index = 0; index <= 2; index++) {
181170
final var split = new TestSplit(index,
182171
3,
@@ -186,12 +175,10 @@ void run_withJUnit_withThreeSplits() throws Exception {
186175
tmp,
187176
true,
188177
exitCode::set);
189-
split.run();
190-
// add a new line to separate the captured output
191-
System.out.println();
178+
splits.add(split.run());
192179
}
193180

194-
assertThat(systemOut.getLines()).hasSize(3).containsSequence( //
181+
assertThat(splits).hasSize(3).containsExactly( //
195182
"de.donnerbart.example.SlowestTest",
196183
"de.donnerbart.example.SlowTest",
197184
"de.donnerbart.example.FastTest de.donnerbart.example.NoTimingOneTest de.donnerbart.example.NoTimingTwoTest");
@@ -200,6 +187,7 @@ void run_withJUnit_withThreeSplits() throws Exception {
200187

201188
@Test
202189
void run_withJUnit_withFourSplits() throws Exception {
190+
final var splits = new ArrayList<String>();
203191
for (int index = 0; index <= 3; index++) {
204192
final var split = new TestSplit(index,
205193
4,
@@ -209,12 +197,10 @@ void run_withJUnit_withFourSplits() throws Exception {
209197
tmp,
210198
true,
211199
exitCode::set);
212-
split.run();
213-
// add a new line to separate the captured output
214-
System.out.println();
200+
splits.add(split.run());
215201
}
216202

217-
assertThat(systemOut.getLines()).hasSize(4).containsSequence( //
203+
assertThat(splits).hasSize(4).containsExactly( //
218204
"de.donnerbart.example.SlowestTest",
219205
"de.donnerbart.example.SlowTest",
220206
"de.donnerbart.example.FastTest",
@@ -231,18 +217,16 @@ void run_whitespaceClassDefinition() throws Exception {
231217
"WhitespaceClassDefinitionTest.java",
232218
PERMISSIONS);
233219

234-
final var testSplit = new TestSplit(0,
220+
final var split = new TestSplit(0,
235221
1,
236222
"**/multiline-class-definition-project/**/*Test.java",
237223
null,
238224
null,
239225
projectFolder,
240226
true,
241227
exitCode::set);
242-
testSplit.run();
243228

244-
assertThat(systemOut.getLines()).singleElement()
245-
.isEqualTo("de.donnerbart.example.WhitespaceClassDefinitionTest");
229+
assertThat(split.run()).isEqualTo("de.donnerbart.example.WhitespaceClassDefinitionTest");
246230
assertThat(exitCode).hasNullValue();
247231
}
248232

@@ -255,17 +239,16 @@ void run_thirdPartyLibrary() throws Exception {
255239
"ThirdPartyLibraryTest.java",
256240
PERMISSIONS);
257241

258-
final var testSplit = new TestSplit(0,
242+
final var split = new TestSplit(0,
259243
1,
260244
"**/third-party-library-project/**/*Test.java",
261245
null,
262246
null,
263247
projectFolder,
264248
true,
265249
exitCode::set);
266-
testSplit.run();
267250

268-
assertThat(systemOut.getLines()).singleElement().isEqualTo("de.donnerbart.example.ThirdPartyLibraryTest");
251+
assertThat(split.run()).isEqualTo("de.donnerbart.example.ThirdPartyLibraryTest");
269252
assertThat(exitCode).hasNullValue();
270253
}
271254

@@ -274,35 +257,33 @@ void run_noPackage() throws Exception {
274257
final var projectFolder = tmp.resolve("no-package-project").resolve("src").resolve("main").resolve("java");
275258
copyResourceToTarget(projectFolder, "tests/NoPackageTest.java", "NoPackageTest.java", PERMISSIONS);
276259

277-
final var testSplit = new TestSplit(0,
260+
final var split = new TestSplit(0,
278261
1,
279262
"**/no-package-project/**/*Test.java",
280263
null,
281264
null,
282265
projectFolder,
283266
true,
284267
exitCode::set);
285-
testSplit.run();
286268

287-
assertThat(systemOut.getLines()).singleElement().isEqualTo("NoPackageTest");
269+
assertThat(split.run()).isEqualTo("NoPackageTest");
288270
assertThat(exitCode).hasNullValue();
289271
}
290272

291273
@Test
292274
void run_noTests() throws Exception {
293275
final var projectFolder = tmp.resolve("no-tests-project").resolve("src").resolve("main").resolve("java");
294276

295-
final var testSplit = new TestSplit(0,
277+
final var split = new TestSplit(0,
296278
1,
297279
"**/no-tests-project/**/*Test.java",
298280
null,
299281
null,
300282
projectFolder,
301283
true,
302284
exitCode::set);
303-
testSplit.run();
304285

305-
assertThat(systemOut.getLinesNormalized()).isEmpty();
286+
assertThat(split.run()).isEmpty();
306287
assertThat(exitCode).hasValue(1);
307288
}
308289

@@ -311,17 +292,16 @@ void run_noClassName() throws Exception {
311292
final var projectFolder = tmp.resolve("no-classname-project").resolve("src").resolve("main").resolve("java");
312293
copyResourceToTarget(projectFolder, "tests/NoClassNameTest.java", "NoClassNameTest.java", PERMISSIONS);
313294

314-
final var testSplit = new TestSplit(0,
295+
final var split = new TestSplit(0,
315296
1,
316297
"**/no-classname-project/**/*Test.java",
317298
null,
318299
null,
319300
projectFolder,
320301
true,
321302
exitCode::set);
322-
testSplit.run();
323303

324-
assertThat(systemOut.getLinesNormalized()).isEmpty();
304+
assertThat(split.run()).isEmpty();
325305
assertThat(exitCode).hasValue(1);
326306
}
327307
}

0 commit comments

Comments
 (0)