Reputation: 504
I'm trying to setup and tear down a database in Play Framework 2.1 test suite using Scalatest
we actually had setup with
running(FakeApplication()){
}
but I would like to setup the database before each individual test, from my understanding of Scalatest you can do this with
override def beforeEach(){
}
So I tried to run a couple of Squeryl queries from within and got some errors about the Session being closed.
So I then tried to create a session within the beforeEach method:
override def beforeEach(){
import org.squeryl.SessionFactory
Class.forName("org.postgresql.Driver").newInstance()
// classOf[org.postgresql.Driver]
DriverManager.registerDriver(new org.postgresql.Driver)
val props = new Properties()
props.setProperty("user","db")
props.setProperty("password","db")
SessionFactory.concreteFactory = Some(()=>
Session.create(
java.sql.DriverManager.getConnection("jdbc:postgresql://127.0.0.1/db", props),
new PostgreSqlAdapter))
CloudUsers.truncateUsers()
Servers.truncateServers()
}
This has cleared out the session errors but I now get:
Could not run test Controllers.UserTest: java.lang.ExceptionInInitializerError
Throwable escaped the test run of 'Controllers.UserTest': java.lang.ExceptionInInitializerError
java.lang.ExceptionInInitializerError
....
Caused by: java.lang.RuntimeException: There is no started application
So in a nutshell, can I run a beforeEach setup method in Play or do I just have to munge it with some bootstrap at the top of each test?
Thanks
Tom
Upvotes: 1
Views: 1085
Reputation: 504
Had a sleep on it then tried putting
running(FakeApplication()) {
}
within the
override def beforeEach(){
}
code, which seems to do the trick, not sure of the implications this has, if any, on the Play Framework, but does the job for now.
Upvotes: 2
Reputation: 21567
beforeEach method from BeforeAndAfterEach trait can be used only in ScalaTest tests, but not in the application itself.
it can be mixed into suites that need methods invoked before and after running each test.
So yes, this fixture was created to make some settings before each test will be run by the suite. You can read about this and some other ScalaTest fixtures here
Upvotes: 0