Skip to content

fix: allow overriding testKit in ScalaTestWithActorTestKit - #3610

Open
pjfanning wants to merge 3 commits into
apache:mainfrom
pjfanning:fix-scalatest-testkit-override-1140
Open

pjfanning wants to merge 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
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

1 participant