You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
0 commit comments