Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullPointerException when overriding testKit from ScalaTestWithActorTestKit #1140

Open
ccerbusca opened this issue Feb 22, 2024 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@ccerbusca
Copy link

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

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

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.

@jxnu-liguobin jxnu-liguobin added the bug Something isn't working label Feb 22, 2024
@mdedetrich
Copy link
Contributor

mdedetrich commented Feb 22, 2024

I don't think this is generally solvable due to how trait initialization works in Scala 2, long story short trait composition in Scala 2 is not commutative which means that the order of when you mix in traits can effect initialization which can lead to NullPointerException in cases like this. For these reason its not really Pekko specific.

The solution to this problem is to use lazy val/def when you override the field. Also afaik this is not an issue in Scala 3 where the compiler will re-order the fields in traits to make sure its initialized correctly.

@jxnu-liguobin
Copy link
Member

jxnu-liguobin commented Feb 24, 2024

Can we add documentation to explain it?Although this is an initialization pit.

@mdedetrich
Copy link
Contributor

mdedetrich commented Feb 24, 2024

Can we add documentation to explain it?Although this is an initialization pit.

Sure, although I wouldn't spend too much time on it because as said previously it really has nothing specifically to do with Pekko.

I would propose adding a single line in the docs briefly outlining the problem containing a link to an official Scala 2 source explaining this.

I will also remove the bug label for this reason.

@mdedetrich mdedetrich added documentation Improvements or additions to documentation and removed bug Something isn't working labels Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants