How to properly test with a populated database?

47 views
Skip to first unread message

Stefan Franck

unread,
Aug 30, 2015, 6:37:19 AM8/30/15
to play-framework
Hi,

I'm rather new to the play framework, and currently trying to prepare and test a model. I'm wondering how to populate a database and retrieve the values put into it for e.g. testing find methods. Here's what I'm doing at the moment:

I extended withApplication to prepare data setup: 

class WithDBApplication(app: Application) extends WithApplication {
  
  override def around[T: AsResult](t: => T): Result = super.around {
    setupData()
    t
  }
  
  def setupData() = {
    val items   = Generator.generateTestItems()    
   }
}

The generateTestItems basically creates items, persists them in the DB and returns a list of the generated items to test e.g. look-ups.

In my test case, I'm trying to do the following:
class ItemServiceTest extends Specification {

  "ItemService.findById" should {
    "find existing id" in new WithDBApplication(app) {
      val testItem = ???
      ItemService.findById(testItem.id) must beSome[Item].which {
        item => item == testItem
      }
    }
  }
}

For the testItem, I want to access one of the items the DB has been populated with. How can I access these items? Or am I doing something completely wrong in this setup?

I'm running play framework 2.3.

Thanks for any help, 
Stefan

Stefan Franck

unread,
Aug 30, 2015, 8:29:06 AM8/30/15
to play-framework
In the meantime I found this blog post, which seems to deal with a similar issue:

http://thelearningcollective.nyc/adventures-in-setupteardown-with-specs2-play/

But I agree with the author, there seems to be an awful lot of boilerplate code. Is it possible to achieve the same result with less clutter?
Reply all
Reply to author
Forward
0 new messages