在Play 2.6中测试控制器时出现NullPointerException

时间:2018-05-22 12:45:21

标签: scala unit-testing playframework nullpointerexception play-reactivemongo

我正在使用ReactiveMongo的Scala Play 2.6应用程序。我已经按照默认Play 2.6安装的HomeControllerSpec.scala示例(play-scala-seed.g8)来测试我的ProjectController,方法是创建一个新的实例,但我得到的是NullPointerException

这是我的控制人员:

class ProjectController @Inject()(
      cc: ControllerComponents,
      val reactiveMongoApi: ReactiveMongoApi
  )(implicit ec: ExecutionContext) extends AbstractController(cc) with I18nSupport {

  def collection: Future[JSONCollection] = reactiveMongoApi.database.map(
    _.collection[JSONCollection]("project")
  )

  def listProjects: Action[AnyContent] = Action.async { implicit request =>
    val futureProjectsList: Future[List[Project]] = collection.flatMap(
      _.find(Json.obj())
      .projection(Json.obj())
      .cursor[Project]()
      .collect[List](-1, Cursor.FailOnError[List[Project]]())
    )

    for {
      projectsList <- futureProjectsList
    } yield {
      Ok(views.html.project.list(projectsList))
    }
  }
}

这是我的考验:

class ProjectControllerSpec extends PlaySpec
  with GuiceOneAppPerTest
  with Injecting
  with MockitoSugar {

  val mockedReactiveMongoApi: ReactiveMongoApi = mock[ReactiveMongoApi]
  val mockedExecutionContext: ExecutionContext = mock[ExecutionContext]

  "ProjectController GET" should {
    "render the 'List of Projects' page from a new instance of controller" in {
      val controller: ProjectController = new ProjectController(
                                                      stubControllerComponents(),
                                                      mockedReactiveMongoApi
                                                    )(mockedExecutionContext) {
        override def collection: Future[JSONCollection] = mock[Future[JSONCollection]]
      }
      val result = controller.listProjects().apply(FakeRequest(GET, "/projects"))

      status(result) mustBe OK
      contentType(result) mustBe Some("text/html")
      contentAsString(result) must include ("Projects")
    }
  }
}

但是当我进行测试时,我得到了:

[info]   java.lang.NullPointerException:
[info]   at controllers.ProjectController.$anonfun$listOfProjects$1(ProjectController.scala:51)
[info]   at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:482)
[info]   at play.api.mvc.ActionBuilderImpl.invokeBlock(Action.scala:480)
[info]   at play.api.mvc.ActionBuilder$$anon$2.apply(Action.scala:419)
[info]   at controllers.ProjectControllerSpec.$anonfun$new$2(ProjectControllerSpec.scala:40)
[info]   at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:20)

我还为这个控制器运行了2个其他测试,然后传递(通过注入控制器从应用程序渲染页面,从路由器渲染)。那么我怎么能通过这个测试呢?提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

NullPointerException来自使用没有定义行为的模拟对象。因此,当执行到达reactiveMongoApi.database时,这将导致异常。

这个github project引入了一种模仿反应性mongo api的方式,但它不再被维护了。

您还可以查看允许您模拟mongo连接的acolyte

顺便说一句,你不应该对ExecutionContext使用模拟,否则你的Future将无效