尝试在 Scala 的 Play 2.6 中测试控制器时出现空指针异常。这是对 OK 响应的测试:
class ApplicationControllerSpec extends PlaySpec
with MockitoSugar with ScalaFutures {
val mockOrchestrator = mock[ApplicationOrchestrator]
val mockCC = mock[ControllerComponents]
val controller = new ApplicationController(mockOrchestrator, mockCC)
val method = controller.home()(FakeRequest())
assert(status(method) == 200)
}
这是我正在测试的控制器:
class ApplicationController @Inject()
(orchestrator: ApplicationOrchestrator, cc: ControllerComponents)
extends AbstractController(cc) with I18nSupport {
def home(): Action[AnyContent] = Action {
implicit request: RequestHeader => //line 29
Ok(views.html.home())
}
}
该错误看起来与隐式请求相关,但我找不到解决方案。
日志输出为:
java.lang.NullPointerException was thrown.
java.lang.NullPointerException
at controllers.ApplicationController.home(ApplicationController.scala:29)