Play / Scala injection controller in test

So, according to the Play 2.4 documentation ( https://playframework.com/documentation/2.4.x/ScalaTestingWithScalaTest#Unit-Testing-Controllers ), the controller should be configured as a trait like this

trait ExampleController { this: Controller => def index() = Action { Ok("ok") } } object ExampleController extends Controller with ExampleController 

so that the test works this way

 class ExampleControllerSpec extends PlaySpec with Results { class TestController() extends Controller with ExampleController "Example Page#index" should { "should be valid" in { //test code } } } 

however, I am using Guice dependency injection, and according to the Play 2.4 documentation ( https://playframework.com/documentation/2.4.x/ScalaDependencyInjection ) my controller looks like this:

 @Singleton class ExampleController @Inject() (exampleService: IExampleService) extends Controller { def index() = Action { Ok("") } } 

Since the controller is no longer a sign, and I can not mix it with the test as follows: with ExampleController , how do I make the test higher?

+5
source share
1 answer

You can inherit directly from ExampleController. You can also eliminate extends Controller , as your controller already inherits this:

 class TestController(service: IExampleService) extends ExampleController(service) 

More information on testing can be found using the Play and Guice feature here.

+3
source

Source: https://habr.com/ru/post/1236326/


All Articles