Java – test websocket in the playframework
•
Java
There is a websocket in my play application. I want to write a test for it, but I can't find an example of how to write such a test I found a discussion in the play framework Google Group, but there has been no activity recently
So, do you have any ideas on how to test websocket in Java testing?
Solution
You can retrieve the underlying iteratee enumerator and test them directly This eliminates the need for a browser You need akka - testkit to deal with the asynchronous nature of iterations
A Scala example:
object WebSocket extends Controller { def websocket = WebSocket.async[JsValue] { request => Future.successful(Iteratee.ignore[JsValue] -> Enumerator.apply[JsValue](Json.obj("type" -> "error"))) } } class WebSocketSpec extends PlaySpecification { "WebSocket" should { "respond with error packet" in new WithApplication { val request = FakeRequest() var message: JsValue = null val iteratee = Iteratee.foreach[JsValue](chunk => message = chunk)(Akka.system.dispatcher) Controller.websocket().f(request)(Enumerator.empty[JsValue],iteratee) TestKit.awaitCond(message == Json.obj("type" -> "error"),1 second) } } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码