apache / apache/pekko

NullPointerException when overriding testKit from ScalaTestWithActorTestKit

Open
#1,140 3 comments 0 reactions 0 assignees View on GitHub
documentation
Dominant language
Scala
Stars
1.6k
Forks
211
Avg merge
1d 6h
Merged PRs (30d)
89

Description

While migrating a project to Pekko, I noticed that some of the tests started failing with `NullPointerException`s.

On further investigation, it was because I was overriding the TestKit inside those tests.
Here's a simple reproducer:

```scala
import org.apache.pekko.actor.testkit.typed.scaladsl.{ActorTestKit, ScalaTestWithActorTestKit}
import org.scalatest.funsuite.AnyFunSuiteLike

class Test extends ScalaTestWithActorTestKit with AnyFunSuiteLike {

override val testKit = ActorTestKit()

test("test") {
assert(true)
}

}
```

I think the issue seems to be this line, where the `val` is not lazy, leading to a NullPointerException.

https://github.com/apache/incubator-pekko/blob/28314e679885519f6458ebf10f9a181d5299c83a/actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestWithActorTestKit.scala#L94

I understand that in this case I should have passed the `TestKit` to the `ScalaTestWithActorTestKit` constructor (which I do now), but having the ability to override `testKit`, makes it a possible usage for other people too.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.