Commit 7f3c011
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 <andreas.turban@net-baustelle.de>1 parent f0f60ab commit 7f3c011
2 files changed
Lines changed: 7 additions & 3 deletions
File tree
- docs
- spock-core/src/main/java/org/spockframework/runtime
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
| 63 | + | |
63 | 64 | | |
64 | 65 | | |
65 | 66 | | |
| |||
Lines changed: 6 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
68 | 68 | | |
69 | 69 | | |
70 | 70 | | |
71 | | - | |
72 | | - | |
73 | | - | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
74 | 77 | | |
75 | 78 | | |
76 | 79 | | |
| |||
0 commit comments