4

I'm using Play 2.6.x and the test helper for status(result) has the method:
def status(of: Accumulator[ByteString, Result])(implicit timeout: Timeout, mat: Materializer): Int = status(of.run())

Running tests throws when the compiler can't find the implicit value: could not find implicit value for parameter mat: akka.stream.Materializer

What is the Materializer -- I'm assuming it's part of Akka-HTTP

And how can I provide one?

tgk
  • 3,857
  • 2
  • 27
  • 42

4 Answers4

5

From akka streams docs:

The Materializer is a factory for stream execution engines, it is the thing that makes streams run [...]

The Materializer is the cornerstone of Akka Streams, on which Akka HTTP is built on. You need one of these to be implicitly resolved to make your test compile.

Presently the ActorMaterializer is the only available implementation of Materializer. It is a Materializer based on Akka actors. This is the reason why, to create one, you need in turn to have an ActorSystem in scope.

The following code is what you need in your test:

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
implicit val sys = ActorSystem("MyTest")
implicit val mat = ActorMaterializer()
Richard Gomes
  • 5,675
  • 2
  • 44
  • 50
Stefano Bonetti
  • 8,973
  • 1
  • 25
  • 44
  • Stefano - I am doing this but I am getting NoMaterializer exception when I run my test case. Could you please take a look at https://stackoverflow.com/questions/57577155/getting-unsupportedoperationexception-nomaterializer-does-not-provide-an-execu – Manu Chadha Aug 20 '19 at 16:06
2

there's also a status method in the form:

def status(of: Future[Result])(implicit timeout: Timeout): Int

make sure the controller return type is correct so the action returns a Future[Result]

LiorH
  • 18,524
  • 17
  • 70
  • 98
  • i tried this -- it still required the Materializer in order to produce a `Future[Result]` using `controller.method().apply(FakeRequest()).run()` unless you know some other way? – tgk Dec 30 '17 at 22:32
  • yes, try `controller.method()(FakreRequest())` no need apply or run – LiorH Dec 31 '17 at 06:55
1

How about doing this:

implicit val materializer = ActorMaterializer()
joesan
  • 13,963
  • 27
  • 95
  • 232
  • awesome thanks! this worked but i still needed to add an implicit `ActorRefFactory` within scope using as @stefano mentioned above `implicit val sys = ActorSystem("MyTest")` – tgk Dec 30 '17 at 22:34
1

As of Play 2.6.0 ActorMaterializer() is deprecated, but you can do this instead:

 val as = ActorSystem()
 implicit val materializer = Materializer(as)
Peewee 733
  • 31
  • 6