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

Testkit support (or examples) for Akka Typed #2036

Closed
chbatey opened this issue May 29, 2018 · 3 comments
Closed

Testkit support (or examples) for Akka Typed #2036

chbatey opened this issue May 29, 2018 · 3 comments
Assignees
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted 2 - pick next Used to mark issues which are next up in the queue to be worked on. The tag is non-binding discuss Tickets that need some discussion before proceeding t:testing Issues related to the akka-http-testkit module
Milestone

Comments

@chbatey
Copy link
Member

chbatey commented May 29, 2018

RouteTest has a field system that has an untyped ActorSystem and ActorTestKit has a field system that is a typed ActorSystem so they can't be used together.

Perhaps just a docs addition to show that you don't need to use the ActorTestKit from Akka Typed and can just use TestProbes e.g. https://gist.github.com/chbatey/964b80adc2cd124fa4bf4624927b5be0

@chbatey chbatey added the discuss Tickets that need some discussion before proceeding label May 29, 2018
@jlprat
Copy link
Member

jlprat commented May 30, 2018

I think it makes sense to add this one to the docs

@babloo80
Copy link

Here is what I did (perhaps naive)...

import akka.actor.{ActorSystem, Scheduler}
import akka.actor.testkit.typed.scaladsl.{ActorTestKit, ActorTestKitBase}
import akka.http.scaladsl.testkit.ScalatestRouteTest
import org.scalatest.Suite
import akka.util.Timeout

trait ScalatestTypedActorHttpRoute extends ScalatestRouteTest { this: Suite =>
  import akka.actor.typed.scaladsl.adapter._

  var typedTestKit:ActorTestKit = _ //val init causes createActorSystem() to cause NPE when typedTestKit.system is called in createActorSystem().
  implicit def timeout: Timeout = typedTestKit.timeout
  implicit def scheduler: Scheduler = typedTestKit.scheduler

  protected override def createActorSystem(): ActorSystem = {
    typedTestKit = ActorTestKit(ActorTestKitBase.testNameFromCallStack())
    typedTestKit.system.toUntyped
  }

  override def cleanUp(): Unit = {
    super.cleanUp()
    typedTestKit.shutdownTestKit()
  }
}

In the test itself...

class TokenLookupServiceWSSpec extends FlatSpec with Matchers with ScalatestTypedActorHttpRoute {
  implicit val mat: ActorMaterializer = ActorMaterializer()
  val wsClient = WSProbe()
  val typedActorRef = typedTestKit.spawn(SomeUsefulTypedActor())
 
  "test with WS" should "be handled correctly" in {
    WS("/ws", wsClient.flow) ~> myroute(typedActorRef) ~>
    check {
      wsClient.sendMessage(newTokenCreationRequest)
       toJSON(wsClient.expectMessage()) shouldBe(someThingUseful())
    }
  } 
}

@jrudolph jrudolph added this to the 10.2.0 milestone Jul 30, 2020
@jrudolph jrudolph added 1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted t:testing Issues related to the akka-http-testkit module labels Jul 30, 2020
@jrudolph jrudolph modified the milestones: 10.2.0, 10.2.1 Aug 6, 2020
@raboof raboof added the 2 - pick next Used to mark issues which are next up in the queue to be worked on. The tag is non-binding label Oct 1, 2020
@raboof raboof self-assigned this Oct 1, 2020
@raboof
Copy link
Member

raboof commented Oct 2, 2020

Added examples in #3496. It might still be nice to improve the API support for this, but let's track that in a separate ticket.

@raboof raboof closed this as completed Oct 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 - triaged Tickets that are safe to pick up for contributing in terms of likeliness of being accepted 2 - pick next Used to mark issues which are next up in the queue to be worked on. The tag is non-binding discuss Tickets that need some discussion before proceeding t:testing Issues related to the akka-http-testkit module
Projects
None yet
Development

No branches or pull requests

5 participants