Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Use Evolutions. Write a setup and teardown script for the fixtures in SQL, or use mysqldump (or equivalent for your DB) to export an existing test DB as sql.</p> <p><a href="http://www.playframework.org/documentation/1.2/evolutions" rel="noreferrer">http://www.playframework.org/documentation/1.2/evolutions</a></p> <p>I find the most stress-free way to do testing is to set everything up in an in-memory database which means tests run fast and drive the tests from Java using JUnit. I use H2DB, but there are a few gotchas you need to watch out for. I learned these the hard way, so this should save you some time.</p> <p>Play has a nice system for setting up and tearing down your application for integration testing, using running( FakeAplication() ) { .. }, and you can configure it to use an in memory database with FakeApplication(additionalConfiguration = inMemoryDatabase()) see:</p> <p><a href="http://www.playframework.org/documentation/2.0/ScalaTest" rel="noreferrer">http://www.playframework.org/documentation/2.0/ScalaTest</a></p> <p><strong>OutOfMemory errors:</strong> However, running a sizeable test fixture a few times on my machine caused OutOfMemory errors. This seems to be because the default implementation of the inMemoryDatabase() function creates a new randomly named database and doesn't clean up the old ones between test runs. This isn't necessary if you've written your evolution teardown scripts correctly, because the database will be emptied out and refilled between each test. So we overrode this behaviour to use the same database and the memory issues disappeared.</p> <p><strong>DB Dialect:</strong> Another issue is that our production database is MySQL which has a number of incompatibilities with H2DB. H2DB has compatibility modes for a number of dbs, which should reduce the number of problems you have: </p> <p><a href="http://www.h2database.com/html/features.html#compatibility" rel="noreferrer">http://www.h2database.com/html/features.html#compatibility</a></p> <p>Putting this all together makes it a little unwieldy to add before each test, so I extracted it into a function:</p> <pre><code>def memDB[T](code: =&gt;T) = running( FakeApplication( additionalConfiguration = Map( "db.default.driver" -&gt; "org.h2.Driver", "db.default.url" -&gt; "jdbc:h2:mem:test;MODE=MySQL" ) ) )(code) </code></pre> <p>You can then use it like so (specs example):</p> <pre><code>"My app" should { "integrate nicely" in memDB { ..... } } </code></pre> <p>Every test will start a fake application, run your fixture setup evolutions script, run the test, then tear it all down again. Good luck!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload