Skip to content

NPE in SpecRunHistory.sortFeatures due to missing duration in run-history #2234

Closed

Description

@ThanosTsiamis

Describe the bug

Spock throws a NullPointerException (NPE) during test discovery when OptimizeRunOrderExtension tries to sort features using a stale or partially written .spock/RunHistory file.
The issue occurs because SpecRunHistory.sortFeatures assumes that featureDurations.get(featureName) is never null.

Stacktrace:

Cannot invoke "java.lang.Long.longValue()" because the return value of "java.util.Map.get(Object)" is null
java.lang.NullPointerException: Cannot invoke "java.lang.Long.longValue()" because the return value of "java.util.Map.get(Object)" is null
	at org.spockframework.runtime.SpecRunHistory.lambda$sortFeatures$0(SpecRunHistory.java:72)
	at java.base/java.util.TimSort.countRunAndMakeAscending(TimSort.java:355)
	at java.base/java.util.TimSort.sort(TimSort.java:220)
	at java.base/java.util.Arrays.sort(Arrays.java:1308)
	at java.base/java.util.ArrayList.sort(ArrayList.java:1804)
	at org.spockframework.runtime.model.SpecInfo.sortFeatures(SpecInfo.java:358)
	at org.spockframework.runtime.SpecRunHistory.sortFeatures(SpecRunHistory.java:61)
	at org.spockframework.runtime.extension.builtin.OptimizeRunOrderExtension.visitSpec(OptimizeRunOrderExtension.java:41)
	at org.spockframework.runtime.ExtensionRunner.runGlobalExtensions(ExtensionRunner.java:49)
	at org.spockframework.runtime.ExtensionRunner.run(ExtensionRunner.java:43)
	at org.spockframework.runtime.SpockEngineDiscoveryPostProcessor.processSpecNode(SpockEngineDiscoveryPostProcessor.java:52)
	at org.spockframework.runtime.SpockEngineDiscoveryPostProcessor.lambda$postProcessEngineDescriptor$0(SpockEngineDiscoveryPostProcessor.java:16)
	at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
	at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
	at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1939)
	at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:509)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
	at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
	at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
	at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
	at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:596)
	at org.spockframework.runtime.SpockEngineDiscoveryPostProcessor.postProcessEngineDescriptor(SpockEngineDiscoveryPostProcessor.java:17)
	at org.spockframework.runtime.SpockEngine.discover(SpockEngine.java:31)

To Reproduce

  1. Run a Spock test suite with spock.optimizeRunOrder=true enabled.

  2. Interrupt the run midway to force a partial write to the system (e.g., force stop Gradle or crash IDE).

Note:

  • While it’s technically possible to trigger this by interrupting the JVM or killing Gradle during a test run,
    this behavior depends on multiple external factors (e.g. OS buffering, branch switches during Gradle sync).
    Editing the file directly is a much more deterministic way to reproduce the issue.

The .spock/RunHistory/ file is now partially written — it contains featureConfidences but missing some featureDurations.

  1. Run the same suite again → NPE during discovery (before executing any tests)

Expected behavior

Spock should gracefully handle missing or incomplete entries in .spock/RunHistory files —
for example, if a feature has a recorded confidence but no duration, the optimizer should simply skip or deprioritize it rather than throwing an exception.

The test discovery phase should complete normally, allowing tests to execute as usual.

Actual behavior

When spock.optimizeRunOrder=true is enabled and a .spock/RunHistory/ file contains a feature with a missing duration entry,
Spock throws a NullPointerException during test discovery, before any tests are executed:

Cannot invoke "java.lang.Long.longValue()" because the return value of "java.util.Map.get(Object)" is null

The exception occurs in:

org.spockframework.runtime.SpecRunHistory.lambda$sortFeatures$0(SpecRunHistory.java:72)

Deleting the affected .spock/RunHistory file allows tests to run again, confirming the corruption tolerance issue.

Java version

JDK version: 17 (also reproducible on 21)

Buildtool version

./gradlew --version

------------------------------------------------------------
Gradle 8.14
------------------------------------------------------------

Build time:    2025-04-25 09:29:08 UTC
Revision:      34c560e3be961658a6fbcd7170ec2443a228b109

Kotlin:        2.0.21
Groovy:        3.0.24
Ant:           Apache Ant(TM) version 1.10.15 compiled on August 25 2024
Launcher JVM:  21.0.5 (BellSoft 21.0.5+11-LTS)
Daemon JVM:    /RedactedForPrivacy/.sdkman/candidates/java/21.0.5-librca (no JDK specified, using current Java home)
OS:            Mac OS X 15.6.1 aarch64

What operating system are you using

Mac

Dependencies

This issue is not tied to a specific dependency set or project.

It has been observed across multiple independent Gradle-based projects (Spring Boot, plain Groovy, and mixed Java–Groovy modules) with no shared dependencies between them.
The common factor is simply the use of Spock with spock.optimizeRunOrder=true.

A full dependency list cannot be provided due to internal privacy restrictions,
but the issue is easily reproducible in any minimal Spock setup — including one using only:

testImplementation "org.spockframework:spock-core:2.4-M1-groovy-3.0"

Additional context

No response

Activity

  1. added this to the 2.4-M7 milestone on Oct 21, 2025
  2. added theissue type on Oct 21, 2025
  3. added a commit that references this issue on Oct 21, 2025
    7f3c011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions