You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@pekko.apache.org by "ccerbusca (via GitHub)" <gi...@apache.org> on 2024/02/22 15:23:26 UTC

[I] NullPointerException when overriding testKit from ScalaTestWithActorTestKit [incubator-pekko]

ccerbusca opened a new issue, #1140:
URL: https://github.com/apache/incubator-pekko/issues/1140

   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [I] NullPointerException when overriding testKit from ScalaTestWithActorTestKit [incubator-pekko]

Posted by "jxnu-liguobin (via GitHub)" <gi...@apache.org>.
jxnu-liguobin commented on issue #1140:
URL: https://github.com/apache/incubator-pekko/issues/1140#issuecomment-1962247804

   Can we add documentation to explain it?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [I] NullPointerException when overriding testKit from ScalaTestWithActorTestKit [incubator-pekko]

Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on issue #1140:
URL: https://github.com/apache/incubator-pekko/issues/1140#issuecomment-1959726961

   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.
   
   The solution to this problem is to use `lazy val`/`def`. 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.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org


Re: [I] NullPointerException when overriding testKit from ScalaTestWithActorTestKit [incubator-pekko]

Posted by "mdedetrich (via GitHub)" <gi...@apache.org>.
mdedetrich commented on issue #1140:
URL: https://github.com/apache/incubator-pekko/issues/1140#issuecomment-1962277728

   > 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 explaining the problem containing a link to an official Scala 2 source explaining this.
   
   I will also remove the bug label for this reason.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@pekko.apache.org
For additional commands, e-mail: notifications-help@pekko.apache.org