mardi 22 août 2017

ScalaTest mocking exception not working

Here is the test I'm trying to run:

      (userService.getPaddleId _)
    .expects(auctionId, userId)
    .returns(Future.exception(new AuctionRegistrationNotFound("")))

  val result = Await.result(service.enrollUserForAuction(auctionId, userId))
  result shouldBe false

The function enrollUserforAuction calls userService.getPaddleId so I need to mock out what getPaddleId returns beforehand. I need to test the case when getPaddleId returns a certain exception, and assert that service.enrollUserForAuction returns false.

But while running the test I got a stacktrace pointing at the returns(Future.exception(new AuctionRegistrationNotFound(""))) line saying that something is wrong.

Here is the stacktrace:

There was 1 failure:
                 1) enrollUserForAuction should return false if the user is not already enrolled to an auction(scala.com.sothebys.auction.services.AuctionServiceSpec)
                 AuctionRegistrationNotFound()
                    at scala.com.engine.auction.services.AuctionServiceSpec$$anonfun$15$$anonfun$apply$mcV$sp$27$$anon$53.<init>(AuctionServiceSpec.scala:625)
                    at scala.com.engine.auction.services.AuctionServiceSpec$$anonfun$15$$anonfun$apply$mcV$sp$27.apply(AuctionServiceSpec.scala:614)
                    at scala.com.engine.auction.services.AuctionServiceSpec$$anonfun$15$$anonfun$apply$mcV$sp$27.apply(AuctionServiceSpec.scala:614)
                    at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
                    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
                    at org.scalatest.Transformer.apply(Transformer.scala:22)
                    at org.scalatest.Transformer.apply(Transformer.scala:20)
                    at org.scalatest.WordSpecLike$$anon$1.apply(WordSpecLike.scala:1078)
                    at org.scalatest.TestSuite$class.withFixture(TestSuite.scala:196)
                    at scala.com.engine.auction.services.AuctionServiceSpec.org$scalamock$scalatest$AbstractMockFactory$$super$withFixture(AuctionServiceSpec.scala:34)
                    at org.scalamock.scalatest.AbstractMockFactory$$anonfun$withFixture$1.apply(AbstractMockFactory.scala:35)
                    at org.scalamock.scalatest.AbstractMockFactory$$anonfun$withFixture$1.apply(AbstractMockFactory.scala:34)
                    at org.scalamock.MockFactoryBase$class.withExpectations(MockFactoryBase.scala:51)
                    at scala.com.engine.auction.services.AuctionServiceSpec.withExpectations(AuctionServiceSpec.scala:34)
                    at org.scalamock.scalatest.AbstractMockFactory$class.withFixture(AbstractMockFactory.scala:34)
                    at scala.com.engine.auction.services.AuctionServiceSpec.withFixture(AuctionServiceSpec.scala:34)
                    at org.scalatest.WordSpecLike$class.invokeWithFixture$1(WordSpecLike.scala:1075)
                    at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
                    at org.scalatest.WordSpecLike$$anonfun$runTest$1.apply(WordSpecLike.scala:1088)
                    at org.scalatest.SuperEngine.runTestImpl(Engine.scala:289)
                    at org.scalatest.WordSpecLike$class.runTest(WordSpecLike.scala:1088)
                    at scala.com.sothebys.auction.services.AuctionServiceSpec.runTest(AuctionServiceSpec.scala:34)
                    at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
                    at org.scalatest.WordSpecLike$$anonfun$runTests$1.apply(WordSpecLike.scala:1147)
                    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:396)
                    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
                    at scala.collection.immutable.List.foreach(List.scala:392)
                    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
                    at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:373)
                    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:410)
                    at org.scalatest.SuperEngine$$anonfun$traverseSubNodes$1$1.apply(Engine.scala:384)
                    at scala.collection.immutable.List.foreach(List.scala:392)
                    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:384)
                    at org.scalatest.SuperEngine.org$scalatest$SuperEngine$$runTestsInBranch(Engine.scala:379)
                    at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:461)
                    at org.scalatest.WordSpecLike$class.runTests(WordSpecLike.scala:1147)
                    at scala.com.engine.auction.services.AuctionServiceSpec.runTests(AuctionServiceSpec.scala:34)
                    at org.scalatest.Suite$class.run(Suite.scala:1147)
                    at scala.com.engine.auction.services.AuctionServiceSpec.org$scalatest$WordSpecLike$$super$run(AuctionServiceSpec.scala:34)
                    at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
                    at org.scalatest.WordSpecLike$$anonfun$run$1.apply(WordSpecLike.scala:1192)
                    at org.scalatest.SuperEngine.runImpl(Engine.scala:521)
                    at org.scalatest.WordSpecLike$class.run(WordSpecLike.scala:1192)
                    at scala.com.engine.auction.services.AuctionServiceSpec.org$scalatest$BeforeAndAfterAll$$super$run(AuctionServiceSpec.scala:34)
                    at org.scalatest.BeforeAndAfterAll$class.liftedTree1$1(BeforeAndAfterAll.scala:213)
                    at org.scalatest.BeforeAndAfterAll$class.run(BeforeAndAfterAll.scala:210)
                    at scala.com.engine.auction.services.AuctionServiceSpec.run(AuctionServiceSpec.scala:34)
                    at org.scalatest.junit.JUnitRunner.run(JUnitRunner.scala:99)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.CompositeRequestRunner.runChild(CompositeRequestRunner.java:66)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConcurrentCompositeRequestRunner$1$1.run(ConcurrentCompositeRequestRunner.java:37)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConcurrentRunnerScheduler.finished(ConcurrentRunnerScheduler.java:91)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConcurrentCompositeRequestRunner$1.evaluate(ConcurrentCompositeRequestRunner.java:46)
                    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
                    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.runLegacy(ConsoleRunnerImpl.java:544)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.run(ConsoleRunnerImpl.java:460)
                    at __shaded_by_pants__.org.pantsbuild.tools.junit.impl.ConsoleRunnerImpl.main(ConsoleRunnerImpl.java:845)
                    at org.pantsbuild.tools.junit.ConsoleRunner.main(ConsoleRunner.java:12)

                 FAILURES!!!
                 Tests run: 27,  Failures: 1

There is most definitely something wrong with returning that exception, and if I'm not wrong it has to deal with its constructor. Any advice?

Aucun commentaire:

Enregistrer un commentaire