[2.0.3] Problem using in-memory database for unit tests

3,309 views
Skip to first unread message

Xavier NOPRE

unread,
Sep 14, 2012, 6:56:47 AM9/14/12
to play-framework
Hi,

I have a unit test like this :

@Test
public void testUpdate() {
running(fakeApplication(inMemoryDatabase()), new Runnable() {
@Override
public void run() {
Project project = new Project(...);
project.save();
...
}
});
}

Without  inMemoryDatabase(), the test is OK, connected to my real "default" database (MySQL).

Adding inMemoryDatabase(), the test is running with an H2 in-memory database, but I have a problem with "sequence" statement :

[error] Test models.ProjectTest.testCreateAndRetrieve failed: Error getting sequence nextval
[error]     at com.avaje.ebean.config.dbplatform.SequenceIdGenerator.getMoreIds(SequenceIdGenerator.java:213)
...
[error]     ...
[error] Caused by: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "SELECT PROJECT_SEQ.NEXTVAL UNION[*] SELECT PROJECT_SEQ.NEXTVAL UNION SELECT PROJECT_SEQ.NEXTVAL ...

If I remove my own "1.sql" evolution file, a new one is generated with statement to create "sequence", but this script is not accepted when running play with my real MySQL database.

What is the solution to run the unit tests with in-memory database, and keeping my own custom evolution scripts for MySQL ?

Thansk,

Xavier

Matthieu

unread,
Sep 14, 2012, 9:34:19 AM9/14/12
to play-fr...@googlegroups.com
Hi Xavier,

The solution is probably to use a specific database configuration name for your tests.

For example, if you use the name "test" instead of "default", the evolution script for test will be generated in "conf/evolutions/test" instead of "conf/evolutions/default". That way, you can keep your own evolution scripts under "default".

To do that, you will have to use a specific configuration file for your tests. You can tell play to use it using the argument "-Dconfig.file=..." when running your application.


Matthieu

Xavier NOPRE

unread,
Sep 14, 2012, 12:14:59 PM9/14/12
to play-fr...@googlegroups.com
Hi Matthieu,

Thank you for your reply !

I had this idea to use other database configuration.

I launch tests like this :

play -Dconfig.file=conf/application.test.conf test

In the conf/application.test.conf file, I have put this lines :

ebean.dbtest="models.*"
db.dbtest.driver=org.h2.Driver
db.dbtest.url="jdbc:h2:mem:play"

And I start my unit test like this :

@Test
public void test() {
running(fakeApplication(inMemoryDatabase("dbtest")), new Runnable() {
@Override
public void run() {
...

I think it can be a good solution, specifying then evolutions SQL scripts with sequence.

But I have this error :

Test models.MyTest.test failed: The default EbeanServer has not been defined? This is normally set via the ebean.datasource.default property. Otherwise it should be registered programatically via registerServer()

If I put the following line in conf/application.test.conf :

ebean.datasource.default=dbtest

I have the following error :

ConfigException$BadPath: path parameter: Invalid path ' - could not find datasource for datasource.default': Token not allowed in path expression: '-' (you can double-quote this token if you really want it here)

Any idea to help me ?

thanks,

Xavier



2012/9/14 Matthieu <matthieu....@gmail.com>

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/2Pf7mqFA5JsJ.
To post to this group, send email to play-fr...@googlegroups.com.
To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.

Xavier NOPRE

unread,
Sep 17, 2012, 7:11:04 AM9/17/12
to play-fr...@googlegroups.com
Hi Vincent,

You're rights ! It works !! Using a specific config file (-Dconfig.file=conf/application.test.conf) with  H2 database and "evolutionplugin=disabled".

But, I still have the problem running the tests under Eclipse ... The solution seems to put setter and getter in all my model object .... :-( ... Any idea ?

Thanks a lot !!!

Xavier


2012/9/14 Vincent Debergue <vdeb...@gmail.com>
Hi,

I use for my part H2 for the tests and MySQL for the main app but I don't use evolutions. I don't think it will be an issue.

and I updated the BaseModelTest to this : (like you I had a problem with the compatibility)

public class BaseModelTest {

public static FakeApplication app;
public static DdlGenerator ddl;
public static EbeanServer server;

@BeforeClass
public static void setup() {
app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
Helpers.start(app);

server = Ebean.getServer("default");

ServerConfig config = new ServerConfig();
config.setDebugSql(true);

ddl = new DdlGenerator((SpiEbeanServer) server, new H2Platform(), config);
}

@AfterClass
public static void stopApp() {
Helpers.stop(app);
}

@Before
public void resetDb() throws IOException {

// drop
String dropScript = ddl.generateDropDdl();
Ebean.execute(Ebean.createCallableSql(dropScript));

// create
String createScript = ddl.generateCreateDdl();
Ebean.execute(Ebean.createCallableSql(createScript));
// insert data
// ...
}

}

With this I had a great gain of time when running unit test and I was able to pass all my test to H2.
Hope this will help.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To view this discussion on the web visit https://groups.google.com/d/msg/play-framework/-/4W4FdgZ_v-0J.
Reply all
Reply to author
Forward
0 new messages