Permalink
Browse files

Merge pull request #3 from JavaPosseRoundup/scala-subcut-squeryl

Initial subcut integration with mocking and Mockito.
  • Loading branch information...
jamesward committed Mar 2, 2013
2 parents 4038cf7 + b11e90f commit 7d3269473a97a5f332302aaa799ce4890deebb34
@@ -10,8 +10,6 @@ object StandardConfig extends NewBindingModule(module => {
import module._
- bind [Session] toSingle {
- Session.create(DB.getConnection(), new H2Adapter)
- }
-
+ bind [Session] toSingle Session.create(DB.getConnection(), new H2Adapter)
+
})
@@ -7,16 +7,16 @@ import play.api.data.Forms.{mapping, text, optional}
import org.squeryl.PrimitiveTypeMode._
import org.squeryl.Session
-import models.{BarDAO, AppDB, Bar}
-import com.escalatesoft.subcut.inject.Injectable
+import models.{BarDAOStandard, BarDAO, AppDB, Bar}
+import com.escalatesoft.subcut.inject._
import play.api.libs.json.Json
import models.BarCompanion._
import configs.StandardConfig
trait ApplicationBase extends Controller with Injectable {
- lazy val barDao = injectOptional[BarDAO] getOrElse new BarDAO()(bindingModule)
-
+ lazy val barDao = injectOptional[BarDAO] getOrElse new BarDAOStandard
+
val barForm = Form(
mapping(
"name" -> text
View
@@ -15,7 +15,13 @@ object BarCompanion {
implicit val barWrites = Json.writes[Bar]
}
-class BarDAO(implicit val bindingModule: BindingModule) extends Injectable {
+trait BarDAO {
+ def getAllBars: List[Bar]
+ def createBar(bar: Bar)
+}
+
+
+class BarDAOStandard(implicit val bindingModule: BindingModule) extends BarDAO with Injectable {
def getAllBars: List[Bar] = {
using(inject[Session]) {
View
@@ -9,6 +9,7 @@ object ApplicationBuild extends Build {
val appDependencies = Seq(
"org.scalatest" %% "scalatest" % "1.9.1" % "test",
+ "org.mockito" % "mockito-all" % "1.9.5" % "test",
"com.escalatesoft.subcut" %% "subcut" % "2.0-SNAPSHOT",
"org.squeryl" %% "squeryl" % "0.9.5-6",
"org.webjars" % "webjars-play" % "2.1.0",
View
@@ -1,33 +1,45 @@
-import controllers.routes
-import models.{AppDB, Bar}
+import com.escalatesoft.subcut.inject.BindingModule
+import configs.StandardConfig
+import controllers.{ApplicationBase, routes}
+import models.{BarDAO, AppDB, Bar}
+import org.mockito.ArgumentMatcher
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
-import org.squeryl.PrimitiveTypeMode.inTransaction
+import org.scalatest.mock.MockitoSugar
+import org.mockito.Mockito._
import play.api.http.ContentTypes.JSON
import play.api.test._
import play.api.test.Helpers._
-class ApplicationSpec extends FlatSpec with ShouldMatchers {
+class ApplicationSpec extends FlatSpec with MockitoSugar with ShouldMatchers {
- "A request to the addBar action" should "respond" in {
- running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
- val result = controllers.Application.addBar(FakeRequest().withFormUrlEncodedBody("name" -> "FooBar"))
- status(result) should equal (SEE_OTHER)
- redirectLocation(result) should equal (Some(routes.Application.index.url))
- }
- }
-
- "A request to the getBars Action" should "respond with data" in {
- running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
- inTransaction(AppDB.barTable insert Bar(Some("foo")))
-
- val result = controllers.Application.getBars(FakeRequest())
- status(result) should equal (OK)
- contentAsString(result) should include("foo")
+ "An add and get through the DAO" should "call add once and get once" in {
+ StandardConfig.modifyBindings { implicit module =>
+ val theTestDAO = mock[BarDAO]
+ module.bind [BarDAO] toSingle theTestDAO
+
+ when(theTestDAO.getAllBars).thenReturn(List(Bar("Bruce's")))
+
+ val application = new TestApplication
+
+ application.addBar(FakeRequest().withFormUrlEncodedBody("name" -> "Bruce's"))
+ val getResult = application.getBars(FakeRequest())
+
+ contentAsString(getResult) should include("Bruce's")
+
+ import org.mockito.Matchers._
+ verify(theTestDAO).createBar(argThat(new ArgumentMatcher[Bar] {
+ def matches(obj: Any): Boolean = obj match {
+ case bar: Bar => bar.name == "Bruce's"
+ }
+ }))
+
+ verify(theTestDAO).getAllBars
}
}
-
+
+ class TestApplication(implicit val bindingModule: BindingModule) extends ApplicationBase
}
View
@@ -1,4 +1,6 @@
-import models.{AppDB, Bar}
+import com.escalatesoft.subcut.inject.Injectable
+import configs.StandardConfig
+import models.{BarDAOStandard, AppDB, Bar}
import org.scalatest.FlatSpec
import org.scalatest.matchers.ShouldMatchers
@@ -9,14 +11,13 @@ import play.api.test._
import play.api.test.Helpers._
class BarSpec extends FlatSpec with ShouldMatchers {
-
- "A Bar" should "be creatable" in {
- running(FakeApplication(additionalConfiguration = inMemoryDatabase())) {
- inTransaction {
- val bar = AppDB.barTable insert Bar(Some("foo"))
- bar.id should not equal(0)
- }
- }
+
+ "A Bar" should "be creatable" in new WithApplication {
+ implicit val bindingModule = StandardConfig
+ val dao = new BarDAOStandard
+ dao.createBar(Bar("Fred"))
+ dao.createBar(Bar("Domino's"))
+ dao.getAllBars.map(_.name) should be (List("Fred", "Domino's"))
}
}

0 comments on commit 7d32694

Please sign in to comment.