Skip to content

fix: allow overriding testKit in ScalaTestWithActorTestKit - #3610

Merged
He-Pin merged 3 commits into
apache:mainfrom
pjfanning:fix-scalatest-testkit-override-1140
Oct 10, 2026
Merged

He-Pin merged 3 commits into
apache:mainfrom
pjfanning:fix-scalatest-testkit-override-1140

Conversation

@pjfanning

@pjfanning pjfanning commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

Motivation

A spec that overrides testKit with a val, e.g. override val testKit = ActorTestKit(), fails with a NullPointerException. ScalaTestWithActorTestKitBase.patience is a strict val that reads testKit.testKitSettings while the trait is being initialized, before the subclass has assigned its overriding testKit.

Modification

  • Make patience a def, so it is computed on access, after construction. A def (rather than a lazy val) keeps existing override implicit val patience overrides compiling.
  • Add ScalaTestWithActorTestKitSpec.

Result

Specs can override testKit with a val, and patience uses the overriding testkit's DefaultTimeout.

Tests

  • actor-testkit-typed/Test/testOnly ...ScalaTestWithActorTestKitSpec without the fix: failed with NullPointerException in ScalaTestWithActorTestKitBase.testKit()
  • actor-testkit-typed/Test/testOnly org.apache.pekko.actor.testkit.typed.scaladsl.* with the fix: 99 passed on 2.13.18 and 3.3.8
  • actor-testkit-typed/mimaReportBinaryIssues (baseline 1.0.0): passed on 2.13.18 and 3.3.8
  • scalafmt run on changed files

References

Fixes #1140

Motivation:
A spec that overrides `testKit` with a `val`, e.g.
`override val testKit = ActorTestKit()`, fails with a
NullPointerException. `ScalaTestWithActorTestKitBase.patience` is a
strict `val` that reads `testKit.testKitSettings` while the trait is
initialized, before the subclass has assigned its overriding `testKit`.

Modification:
Make `patience` a `lazy val` so it is computed on first use, after
construction. Add `ScalaTestWithActorTestKitSpec`.

Result:
Specs can override `testKit` with a `val`, and `patience` uses the
overriding testkit's `DefaultTimeout`.

Tests:
- `actor-testkit-typed/Test/testOnly ...ScalaTestWithActorTestKitSpec`
  without the fix: failed with NullPointerException in
  `ScalaTestWithActorTestKitBase.testKit()`
- `actor-testkit-typed/Test/testOnly org.apache.pekko.actor.testkit.typed.scaladsl.*`
  with the fix: 99 passed on 2.13.18 and 3.3.8
- `actor-testkit-typed/mimaReportBinaryIssues`: passed on 2.13.18 and
  3.3.8
- scalafmt run on changed files

References:
Fixes apache#1140
@pjfanning pjfanning added this to the 2.0.0-M5 milestone Oct 9, 2026
Motivation:
Making `patience` a `lazy val` broke every subclass that overrides it
with `override implicit val patience`, e.g. `ReplicatedShardingSpec`,
because a strict `val` cannot override a `lazy val`. User specs doing
the same would no longer compile.

Modification:
Make `patience` a `def` instead. It is still evaluated on access rather
than during trait initialization, and can be overridden by a `val`,
`lazy val` or `def`.

Result:
Overriding `testKit` with a `val` works and existing `patience`
overrides still compile.

Tests:
- `actor-testkit-typed/testOnly *ScalaTestWithActorTestKitSpec`: passed
- `Test/compile` for cluster-sharding-typed, stream-tests, coordination,
  remote and actor-tests: passed
- `actor-testkit-typed/mimaReportBinaryIssues`: passed
- scalafmt run on changed files

References:
Fixes apache#1140
Motivation:
The Check / Binary Compatibility job fails on Scala 3. Making
`patience` a `def` leaves `ScalaTestWithActorTestKitBase` with no
fields, so Scala 3 no longer emits the trait's synthetic `$init$`.

Modification:
Add a 2.0.x MiMa exclude for `ScalaTestWithActorTestKitBase.$init$`.

Result:
MiMa passes for actor-testkit-typed on Scala 2.13 and 3.

Tests:
- `++3.3.8 actor-testkit-typed/mimaReportBinaryIssues`: failed without
  the filter with the same error as CI, passed with it
- `actor-testkit-typed/mimaReportBinaryIssues` (2.13): passed

References:
Refs apache#1140

@He-Pin He-Pin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Making patience a def correctly avoids the initialization-order NPE when a spec overrides testKit with a val, while keeping existing override implicit val patience source-compatible. The MiMa filter for the dropped Scala 3 $init$ is in place and the new spec pins the behavior.

@He-Pin
He-Pin merged commit 3b1359a into apache:main Oct 10, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullPointerException when overriding testKit from ScalaTestWithActorTestKit

2 participants