3

I have a trait that I want to mock and use that mocked Trait in another Service during testing. The problem is, that I receive a Nullpointerexception when I try to mock the return value of the indexDocuments function.

Testmethod:

"createDemand must return None if writing to es fails" in new WithApplication {
  val demandDraft = DemandDraft(UserId("1"), "socken bekleidung wolle", Location(Longitude(52.468562), Latitude(13.534212)), Distance(30), Price(25.0), Price(77.0))
  val es = mock[ElasticsearchClient]
  val sphere = mock[SphereClient]
  val productTypes = mock[ProductTypes]

  sphere.execute(any[ProductCreateCommand]) returns Future.successful(product)
  productTypes.demand returns ProductTypeBuilder.of("demand", ProductTypeDrafts.demand).build()
  // this line throws the nullpointer exception
  es.indexDocument(any[IndexName], any[TypeName], any[JsValue]) returns Future.failed(new RuntimeException("test exception"))

  val demandService = new DemandService(es, sphere, productTypes)
  demandService.createDemand(demandDraft) must be (Option.empty[Demand]).await
}

Trait:

sealed trait ElasticsearchClient {
  implicit def convertListenableActionFutureToScalaFuture[T](x: ListenableActionFuture[T]): Future[T] = {
    val p = Promise[T]()
    x.addListener(new ActionListener[T] {
      def onFailure(e: Throwable) = p.failure(e)
      def onResponse(response: T) = p.success(response)
    })
    p.future
  }

  lazy val client = createElasticsearchClient()
  def close(): Unit
  def createElasticsearchClient(): Client

  def indexDocument(esIndex: IndexName, esType: TypeName, doc: JsValue): Future[IndexResponse] =
    client.prepareIndex(esIndex.value, esType.value).setSource(doc.toString()).execute()
  def search(esIndex: IndexName, esType: TypeName, query: QueryBuilder): Future[SearchResponse] =
    client.prepareSearch(esIndex.value).setTypes(esType.value).setQuery(query).execute()
}

Exception

[error]    NullPointerException:   (DemandServiceSpec.scala:89)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8$$anon$2$$anonfun$8.apply(DemandServiceSpec.scala:89)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8$$anon$2$$anonfun$8.apply(DemandServiceSpec.scala:89)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8$$anon$2.delayedEndpoint$services$DemandServiceSpec$$anonfun$1$$anonfun$apply$8$$anon$2$1(DemandServiceSpec.scala:89)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8$$anon$2$delayedInit$body.apply(DemandServiceSpec.scala:81)
[error] play.api.test.WithApplication$$anonfun$around$1.apply(Specs.scala:23)
[error] play.api.test.WithApplication$$anonfun$around$1.apply(Specs.scala:23)
[error] play.api.test.PlayRunners$class.running(Helpers.scala:49)
[error] play.api.test.Helpers$.running(Helpers.scala:403)
[error] play.api.test.WithApplication.around(Specs.scala:23)
[error] play.api.test.WithApplication.delayedInit(Specs.scala:20)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8$$anon$2.<init>(DemandServiceSpec.scala:81)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8.apply(DemandServiceSpec.scala:81)
[error] services.DemandServiceSpec$$anonfun$1$$anonfun$apply$8.apply(DemandServiceSpec.scala:81)

Please let me know if you need additional information.

MeiSign
  • 1,487
  • 1
  • 15
  • 39
  • Would you be able to create a test project to reproduce the issue? Can you try to add a type annotation to `Future.failed` so that it is clear that a `Future[IndexResponse]` is expected? Can you try to use Mockito directy instead of specs2 syntax? – Eric Jan 25 '15 at 23:38
  • Adding the IndexResponse as the Future type does not help unfortunately. You can find another small project where probably the same issue is happening here: http://stackoverflow.com/questions/28111015/mockito-stubbing-method-with-value-class-argument-fails-with-nullpointerexceptio This may be a better testproject? I will try to use Mockito directly – MeiSign Jan 26 '15 at 00:31

2 Answers2

2

I found out that the any[] Matchers in the indexDocuments call are the problem. When I replace them with the actual values it works:

"createDemand must return None if writing to es fails and deleteDemand should be called once with correct parameters" in new WithApplication {
  val demandDraft = DemandDraft(UserId("1"), "socken bekleidung wolle", Location(Longitude(52.468562), Latitude(13.534212)), Distance(30), Price(25.0), Price(77.0))
  val es = mock[ElasticsearchClient]
  val sphere = mock[SphereClient]
  val productTypes = mock[ProductTypes]

  sphere.execute(any[ProductCreateCommand]) returns Future.successful(product)
  sphere.execute(any[ProductDeleteByIdCommand]) returns Future.successful(product)
  productTypes.demand returns ProductTypeBuilder.of("demand", ProductTypeDrafts.demand).build()
  es.indexDocument(IndexName("demands"), TypeName("demands"), Json.toJson(demand)) returns Future.failed(new RuntimeException("test exception"))

  val demandService = new DemandService(es, sphere, productTypes)
  demandService.createDemand(demandDraft) must be (Option.empty[Demand]).await
}
MeiSign
  • 1,487
  • 1
  • 15
  • 39
0

I've had this happen a whole bunch and work around it by creating a class (rather than a trait) to feed to mock:

trait SomeTraitYouWantToMock {
  …
}
class MockableSomeTraitYouWantToMock extends SomeTraitYouWantToMock
val whatever = mock[MockableSomeTraitYouWantToMock]
Hugh
  • 8,872
  • 2
  • 37
  • 42
  • I have tried this aswell but it doesnt solve the Nullpointer unfortunately. I think it has something to do with the lazy val in the trait or the implicit conversion... – MeiSign Jan 26 '15 at 09:33