Skip to content

Commit 7f3c011

Browse files
Fix: Prevent NPE in SpecRunHistory.sortFeatures when duration is missing (#2235)
This PR fixes a NullPointerException in SpecRunHistory.sortFeatures that occurs when a .spock/RunHistory file contains a feature with a confidence value but no recorded duration (e.g., due to an interrupted or partially written test run). Fixes #2234 Previously, Spock assumed all featureDurations entries were present and performed direct unboxing to long, which fails when a value is null. ## Root cause ``` long duration1 = data.featureDurations.get(f1.getName()); long duration2 = data.featureDurations.get(f2.getName()); ``` If either featureDurations.get(...) call returns null, the auto-unboxing causes: `Cannot invoke "java.lang.Long.longValue()" because the return value of "java.util.Map.get(Object)" is null` This typically happens when: The previous run was aborted mid-write or during Gradle/IDE synchronization. The .spock/RunHistory file was created by a different version of Spock. The file was partially deleted or corrupted manually. ## Fix A small defensive check ensures safe comparison even if a duration is missing: ``` Long d1 = data.featureDurations.get(f1.getName()); Long d2 = data.featureDurations.get(f2.getName()); if (d1 == null && d2 == null) return 0; if (d1 == null) return 1; // sort missing durations last if (d2 == null) return -1; return Long.compare(d1, d2); ``` This preserves existing behavior and avoids the NPE without changing sort semantics or serialization. --------- Co-authored-by: Andreas Turban <[email protected]>
1 parent f0f60ab commit 7f3c011

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

docs/release_notes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ will now require that the spec is annotated with <<parallel_execution.adoc#isola
6060
* Fix vararg handling in SpyStatic spockIssue:2161[]
6161
* Fix incompatibility with JUnit 6 in OSGi environment spockIssue:2231[]
6262
* OSGi: Pin the `Require-Capability:osgi.ee=JavaSE` to Java `8` spockPull:2233[]
63+
* Fix: Prevent NPE in SpecRunHistory.sortFeatures when duration is missing spockIssue:2234[]
6364

6465
== 2.4-M6 (2025-04-15)
6566

spock-core/src/main/java/org/spockframework/runtime/SpecRunHistory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ public void sortFeatures(SpecInfo spec) {
6868
if (!confidence1.equals(confidence2))
6969
return confidence1 - confidence2;
7070

71-
long duration1 = data.featureDurations.get(f1.getName()); // never null
72-
long duration2 = data.featureDurations.get(f2.getName()); // never null
73-
return duration1 < duration2 ? -1 : 1;
71+
Long d1 = data.featureDurations.get(f1.getName());
72+
Long d2 = data.featureDurations.get(f2.getName());
73+
74+
if (d1 == null) return 1;
75+
if (d2 == null) return -1;
76+
return d1< d2 ? -1 : 1;
7477
});
7578
}
7679

0 commit comments

Comments
 (0)