Advertisement
ps0604

Mocking modules in Play for Scala to test with ScalaTest

Apr 20th, 2017
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.88 KB | None | 0 0
  1. package admin.bank.manage
  2.  
  3. import play.api.mvc._
  4. import main.init.Properties
  5. import Bank._
  6. import login._
  7. import main._
  8. import common._
  9.  
  10. import play.api.inject.guice._
  11. import play.api.inject.bind
  12. import play.api.libs.json._
  13. import org.scalatest._
  14. import org.scalatestplus.play._
  15. import play.api.test._
  16. import play.api.test.Helpers._
  17. import java.time._
  18. import java.time.format._
  19. import scala.concurrent.Future
  20. import scala.concurrent.ExecutionContext.Implicits.global
  21.  
  22. import org.scalatest.mock.MockitoSugar
  23. import org.mockito.Mockito._
  24. import org.mockito.ArgumentMatchers
  25.  
  26. import com.google.inject.AbstractModule
  27. import tasks.etl.MainEtl
  28. import akka.actor._
  29. import com.google.inject._
  30. import com.google.inject.name.Names
  31.  
  32. class ManageBanksTest extends PlaySpec with OneAppPerSuite with MockitoSugar {
  33.  
  34.   //implicit override lazy val app = new GuiceApplicationBuilder().build
  35.  
  36.  
  37.     val modules = Option(new AbstractModule {
  38.        override def configure() = {
  39.          val mockMainETL: MainEtl = mock[MainEtl]
  40.          bind(classOf[ActorRef])
  41.          .annotatedWith(Names.named("mainEtl"))
  42.          .toInstance(mockMainETL)
  43.        }
  44.     })
  45.    
  46.     implicit override lazy val app = new GuiceApplicationBuilder()
  47.     .overrides(modules.map(GuiceableModule.guiceable).toSeq: _*)
  48.     .build
  49.  
  50.  
  51.  
  52.  
  53.   val permCode = 12
  54.  
  55.   val manyResponse = """
  56.        {
  57.          "bnks" : [ {
  58.              "sk":1,
  59.              "nm":"Bank 1",
  60.              "dsc":"Bank 1 description",
  61.              "als":"BK1"
  62.           },
  63.           {
  64.             "sk":2,
  65.             "nm":"Bank 2",
  66.             "dsc":"Bank 2 description",
  67.             "als":"BK2"
  68.           },
  69.           {
  70.             "sk":3,
  71.             "nm":"Bank 3",
  72.             "dsc":"Bank 3 description",
  73.             "als":"BK3"
  74.           }]
  75.        }
  76.        """
  77.  
  78.    val bk1 = BankSeq(1,"Bank 1", Some("Bank 1 description"),"BK1")
  79.    val bk2 = BankSeq(2,"Bank 2", Some("Bank 2 description"),"BK2")
  80.    val bk3 = BankSeq(3,"Bank 3", Some("Bank 3 description"),"BK3")
  81.    val seqMany = Seq(bk1, bk2, bk3)
  82.      
  83.   "Banks Management" should {
  84.     "read many - user authorized (admin)" in {
  85.      
  86.       val mock1 = mock[ManageBanksDAO]
  87.       when(mock1.readMany).thenReturn(Future{seqMany})
  88.    
  89.       val userMock = UserMock.getAdmin
  90.       val props = new Properties
  91.       val auth = UserMock.getAuth(userMock)
  92.       val userRoleMock = UserMock.getManagePerm(permCode)
  93.       val controller = new ManageBanks(auth,props,mock1,userRoleMock)
  94.       val result: Future[Result] = controller.readMany().apply(FakeRequest(POST, "/"))
  95.       val bodyText: String = contentAsString(result)
  96.      
  97.       val access = 2
  98.       val admin = 1
  99.       val jsonManyResponse: JsValue = Json.parse(manyResponse.format(access,admin))
  100.        
  101.       bodyText mustBe jsonManyResponse.toString
  102.     }
  103.   }
  104.  
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement