Advertisement
mitrakov

ScalaTest InvokePrivate

Sep 26th, 2018
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.70 KB | None | 0 0
  1. // Testing private/protected methods in ScalaTest:
  2. // This test is based on https://pastebin.com/eHqiDQWf
  3.  
  4. // Let's test "resultFuture" method which is protected!
  5. "A MyController" when {
  6.   ...
  7.   "resultFuture is called" should {
  8.     "return 5" in {
  9.       controller.resultFuture.futureValue mustBe 5
  10.     }
  11.   }
  12. }
  13.  
  14.         // output:
  15.         // [error]  Access to protected method resultFuture not permitted because
  16.         // [error]  enclosing class TrixSpec is not a subclass of
  17.         // [error]  class MyController in package controllers where target is defined
  18.         // [error]         controller.resultFuture.futureValue mustBe 5
  19.  
  20. // OK, let's mix-in PrivateMethodTester trait and replace "." with "invokePrivate":
  21. class TrixSpec extends ... with PrivateMethodTester {
  22.   "A MyController" when {
  23.     ...
  24.     "resultFuture is called" should {
  25.       "return 5" in {
  26.         val resultFuture: PrivateMethod[Future[Int]] = PrivateMethod[Future[Int]]('resultFuture)
  27.        controller.invokePrivate(resultFuture()).futureValue mustBe 5
  28.      }
  29.    }
  30.  }
  31. }
  32.  
  33. '       // output:
  34.         // [info] TrixSpec:
  35.         // [info] A MyController
  36.         // [info]   when resultFuture is called
  37.         // [info] application - Future started
  38.         // [info] application - Future finished
  39.         // [info]   - should return 5
  40.         // [info] ScalaTest
  41.         // [info] Run completed in 4 seconds, 592 milliseconds.
  42.         // [info] Total number of tests run: 1
  43.         // [info] Suites: completed 1, aborted 0
  44.         // [info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
  45.         // [info] All tests passed.
  46.  
  47.  
  48.  
  49. // full code sample:
  50. import scala.concurrent.duration._
  51. import scala.concurrent.{ExecutionContext, Future}
  52. import scala.language.postfixOps
  53.  
  54. import controllers.MyController
  55. import org.scalatest.PrivateMethodTester
  56. import org.scalatest.concurrent.ScalaFutures._
  57. import org.scalatest.mockito.MockitoSugar
  58. import org.scalatestplus.play.PlaySpec
  59. import play.api.mvc.ControllerComponents
  60. import play.api.test.Helpers
  61.  
  62. class TrixSpec extends PlaySpec with MockitoSugar with PrivateMethodTester {
  63.  
  64.   private val cc: ControllerComponents = Helpers.stubControllerComponents()
  65.   implicit val ec: ExecutionContext = cc.executionContext
  66.   implicit val patienceConfig: PatienceConfig = PatienceConfig(timeout = 5 seconds, interval = 100 milliseconds)
  67.  
  68.   private val controller = new MyController(cc)
  69.  
  70.   "A MyController" when {
  71.     "resultFuture is called" should {
  72.       "return 5" in {
  73.         val resultFuture: PrivateMethod[Future[Int]] = PrivateMethod[Future[Int]]('resultFuture)
  74.        controller.invokePrivate(resultFuture()).futureValue mustBe 5
  75.      }
  76.    }
  77.  }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement