how can I run unit test by java code (question about jpa & dbunit). thks

221 views
Skip to first unread message

lzy.dev

unread,
Jul 15, 2009, 9:25:15 PM7/15/09
to play-framework
Hi:

I'm try to using play! famework to develop same prototype project. And
feel good.

I have an awkward question about using dbunit framework (DBTestCase)
to test jpa module layer code in play! . As we know this unit test is
drived by java code rather than drived by "play test XXX" or
"http://localhost:9000/@tests".

When I run my test case in eclipse by junit plugin (right click menu)
I got exception like this. ("blog sample" project):


javax.persistence.PersistenceException:
org.hibernate.PropertyAccessException: could not get a field value by
reflection getter of models.Post.comments
at
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
at
org.hibernate.ejb.AbstractEntityManagerImpl.persist
(AbstractEntityManagerImpl.java:226)
at play.db.jpa.JPASupport.save(JPASupport.java:118)
at
unit.jpamodels.PostTestCase.testPostSave(PostTestCase.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown
Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at
org.junit.internal.runners.JUnit38ClassRunner.run
(JUnit38ClassRunner.java:81)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
at
org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)
Caused by: org.hibernate.PropertyAccessException: could not get a
field
value by reflection getter of models.Post.comments
at
org.hibernate.property.DirectPropertyAccessor$DirectGetter.get
(DirectPropertyAccessor.java:58)
at
org.hibernate.property.DirectPropertyAccessor$DirectGetter.getForInsert
(DirectPropertyAccessor.java:63)
at
org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValuesToInsert
(AbstractEntityTuplizer.java:294)
at
org.hibernate.tuple.entity.PojoEntityTuplizer.getPropertyValuesToInsert
(PojoEntityTuplizer.java:239)
at
org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValuesToInsert
(AbstractEntityPersister.java:3696)
at
org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate
(AbstractSaveEventListener.java:290)
at
org.hibernate.event.def.AbstractSaveEventListener.performSave
(AbstractSaveEventListener.java:204)
at
org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId
(AbstractSaveEventListener.java:144)
at
org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId
(EJB3PersistEventListener.java:49)
at
org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient
(DefaultPersistEventListener.java:154)
at
org.hibernate.event.def.DefaultPersistEventListener.onPersist
(DefaultPersistEventListener.java:110)
at
org.hibernate.event.def.DefaultPersistEventListener.onPersist
(DefaultPersistEventListener.java:61)
at
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:645)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:619)
at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:623)
at
org.hibernate.ejb.AbstractEntityManagerImpl.persist
(AbstractEntityManagerImpl.java:220)
... 21 more
Caused by: java.lang.IllegalArgumentException: Can not set
java.util.List field models.Post.comments to models.Post
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
(Unknown
Source)
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
(Unknown
Source)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(Unknown Source)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get(Unknown Source)
at java.lang.reflect.Field.get(Unknown Source)
at
org.hibernate.property.DirectPropertyAccessor$DirectGetter.get
(DirectPropertyAccessor.java:55)
... 36 more


I'm test case code at below. "JpaModelsTestCase" base class use to
init
dbunit env. And "PostTestCase" is to test jpa method like save.
1. At JpaModelsTestCase class constructor I have init & start play!
and
call startTx to create jpa session.
2. The "post.save()" in "testPostSave" mehod is KEY question point.

package unit.jpamodels;

import java.io.File;

import org.dbunit.DBTestCase;
import org.dbunit.PropertiesBasedJdbcDatabaseTester;
import org.dbunit.database.DatabaseConfig;
import org.dbunit.ext.oracle.OracleDataTypeFactory;

import play.Play;

public abstract class JpaModelsTestCase extends DBTestCase {

public JpaModelsTestCase(String name) throws Exception {
super(name);
System.setProperty(
PropertiesBasedJdbcDatabaseTester.DBUNIT_DRIVER_CLASS,
"oracle.jdbc.driver.OracleDriver");
System.setProperty(
PropertiesBasedJdbcDatabaseTester.DBUNIT_CONNECTION_URL,
"jdbc:oracle:thin:@127.0.0.1:1521:ORACLE");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_USERNAME,
"local");
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_PASSWORD,
"local");

// Note that for Oracle you must specify the schema name
in uppercase.
System.setProperty(PropertiesBasedJdbcDatabaseTester.DBUNIT_SCHEMA,
"LOCAL");
// Run play! framework and initialize JPA context.
Play.init(new
File("C:/play-1.0-stable6/play_blog_sample"), "");
Play.start();
play.db.jpa.JPAPlugin.startTx(false);
}
@Override
protected void setUpDatabaseConfig(DatabaseConfig config) {
// Enable the qualified table names feature for oracle.
config.setFeature(DatabaseConfig.FEATURE_QUALIFIED_TABLE_NAMES, true);
// Skip Oracle 10g Recyclebin tables.
config.setFeature
(DatabaseConfig.FEATURE_SKIP_ORACLE_RECYCLEBIN_TABLES,
true);
// Setup oracle 10g data type factory.
config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new
OracleDataTypeFactory());
}


package unit.jpamodels;

import java.io.FileInputStream;

import models.Post;

import org.dbunit.Assertion;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.ITable;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;
import org.junit.Test;

public class PostTestCase extends JpaModelsTestCase {

public PostTestCase() throws Exception {
super("PostTest");
}

@Override
protected IDataSet getDataSet() throws Exception {
return new FlatXmlDataSet(new FileInputStream(
"test/unit/jpamodels/blog_empty.xml"));
}

@Override
protected DatabaseOperation getSetUpOperation() throws Exception
{
return DatabaseOperation.CLEAN_INSERT;
}

@Override
protected DatabaseOperation getTearDownOperation() throws
Exception {
return DatabaseOperation.DELETE_ALL;
}

@Test
public void testPostSave() throws Exception {
Post post = new Post("testPostTitle1",
"testPostContent1");
post.save();

IDataSet dbDataSet = getConnection().createDataSet();
ITable dbTable = dbDataSet.getTable("LOCAL.POST");

IDataSet xmlDataSet = new FlatXmlDataSet(new
FileInputStream(
"/test/unit/jpamodels/expected_post_save.xml"));
ITable expectedTable =
xmlDataSet.getTable("LOCAL.POST");

Assertion.assertEquals(expectedTable, dbTable);
}

}

I had try to use eclipse/junit plugin to run sample unit test case.
But
return exception: "The JPA context is not initialized." (at
JPA.java(18)). I can run test case by "play test XXX" &
"http://localhost:9000/@tests" only??

How can I run unit test by unit test case java code in eclipse/junit
directly??
I need help, please! thank you very much.

btw
https://bugs.launchpad.net/play/+bug/276762/+viewstatus
who can tell me, Guillaume Bor how to fix this.

MORE DETAIL thks.

lzy.dev

unread,
Jul 16, 2009, 2:31:46 AM7/16/09
to play-framework
Hi, Guillaume Bor:

I'm try to using play! famework to develop same prototype project.
And
feel good. Thank you very much!!
I need you help, please! thank you very much.

btw
https://bugs.launchpad.net/play/+bug/276762/+viewstatus
how do you fix this.

lzy@china.
2009.7.6
--
This message was sent from Launchpad by the user
lzy (https://launchpad.net/~lzy-dev)
using the "Contact this team" owner link on the play framework
developers team page.
For more information see
https://help.launchpad.net/YourAccount/ContactingPeople

Guillaume Bort

unread,
Jul 16, 2009, 5:52:42 AM7/16/09
to play-fr...@googlegroups.com
Ok,

I fixed things in the current trunk, revision 473.
You should be able to run a DbUnit test case directly in the
integrated TestRunner.
Just use something like xxxxxTest as class name.

If you really want to run a Play test case outside of the Play scope,
things are more complicated.
Consider using play.Invoker.invokeInThread() to run your code, as it
will ensure that all Play stuff is correctly intialized.

However using the integrated Test runner will be simpler.

Guillaume.

lzy.dev

unread,
Jul 16, 2009, 11:23:03 AM7/16/09
to play-framework
Hi, Guillaume Bort

I tried trunk, revision 473. But I faild. :(

At first I download new version TestEngine.java file.
Then, I use ant rebuild a new version play.jar lib.
And I changed test case class name like this: (endsWith "test")

public abstract class JpaModelsTest extends DBTestCase {
.....
}

public class PostTest extends JpaModelsTest {
....
}

but I got same exception again:

javax.persistence.PersistenceException:
org.hibernate.PropertyAccessException: could not get a field value by
reflection getter of models.Post.comments
at
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.persist
(AbstractEntityManagerImpl.java:226)
at play.db.jpa.JPASupport.save(JPASupport.java:118)
at unit.jpamodels.PostTest.testPostSave(PostTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
(UnsafeFieldAccessorImpl.java:146)
at
sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException
(UnsafeFieldAccessorImpl.java:150)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj
(UnsafeFieldAccessorImpl.java:37)
at sun.reflect.UnsafeObjectFieldAccessorImpl.get
(UnsafeObjectFieldAccessorImpl.java:18)
at java.lang.reflect.Field.get(Field.java:358)
at org.hibernate.property.DirectPropertyAccessor$DirectGetter.get
(DirectPropertyAccessor.java:55)
... 36 more


And then I try to use play.Invoker.invokeInThread() to run jpa code
like this: (JpaModelsTest class not change.)

public class PostTest extends JpaModelsTest {
....

private class MyInvocation extends Invocation
{
public String title = null;

public String content = null;

@Override
public void execute() throws Exception {
Post post = new Post("testPostTitle1", "testPostContent1");
post.save();
}
}

@Test
public void testPostSave() throws Exception {

play.Invoker.invokeInThread(new MyInvocation());

// Post post = new Post("testPostTitle1", "testPostContent1");
// post.save();

IDataSet dbDataSet = getConnection().createDataSet();
ITable dbTable = dbDataSet.getTable("LOCAL.POST");

IDataSet xmlDataSet = new FlatXmlDataSet(new FileInputStream(
"/test/unit/jpamodels/expected_post_save.xml"));
ITable expectedTable = xmlDataSet.getTable("LOCAL.POST");

Assertion.assertEquals(expectedTable, dbTable);
}
}


I got exception when run it by eclipes integrated junit Test runner:


play.exceptions.UnexpectedException: Unexpected Error
at play.Invoker$Invocation.onException(Invoker.java:95)
at play.Invoker$Invocation.doIt(Invoker.java:113)
at play.Invoker.invokeInThread(Invoker.java:44)
at unit.jpamodels.PostTest.testPostSave(PostTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run
(JUnit38ClassRunner.java:81)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:45)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)
Caused by: javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: Cannot open connection
at
org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException
(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:41)
at play.db.jpa.JPAPlugin.startTx(JPAPlugin.java:123)
at play.db.jpa.JPAPlugin.beforeInvocation(JPAPlugin.java:94)
at play.Invoker$Invocation.before(Invoker.java:70)
at play.Invoker$Invocation.doIt(Invoker.java:109)
... 21 more
Caused by: org.hibernate.exception.GenericJDBCException: Cannot open
connection
at
org.hibernate.exception.SQLStateConverter.handledNonSpecificException
(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert
(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert
(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert
(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.ConnectionManager.openConnection
(ConnectionManager.java:449)
at org.hibernate.jdbc.ConnectionManager.getConnection
(ConnectionManager.java:167)
at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:142)
at org.hibernate.transaction.JDBCTransaction.begin
(JDBCTransaction.java:85)
at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:
1353)
at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:38)
... 25 more
Caused by: java.sql.SQLException: An attempt by a client to checkout a
Connection has timed out.
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:106)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:65)
at
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection
(C3P0PooledConnectionPool.java:527)
at com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource.getConnection
(AbstractPoolBackedDataSource.java:128)
at
org.hibernate.ejb.connection.InjectedDataSourceConnectionProvider.getConnection
(InjectedDataSourceConnectionProvider.java:46)
at org.hibernate.jdbc.ConnectionManager.openConnection
(ConnectionManager.java:446)
... 30 more
Caused by: com.mchange.v2.resourcepool.TimeoutException: A client
timed out while waiting to acquire a resource from
com.mchange.v2.resourcepool.BasicResourcePool@1083717 -- timeout at
awaitAvailable()
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable
(BasicResourcePool.java:1317)
at
com.mchange.v2.resourcepool.BasicResourcePool.prelimCheckoutResource
(BasicResourcePool.java:557)
at com.mchange.v2.resourcepool.BasicResourcePool.checkoutResource
(BasicResourcePool.java:477)
at
com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection
(C3P0PooledConnectionPool.java:525)
... 33 more


When I add "play.db.jpa.JPAPlugin.startTx(false);" to MyInvocation
class:

private class MyInvocation extends Invocation
{
public String title = null;

public String content = null;

@Override
public void execute() throws Exception {

play.db.jpa.JPAPlugin.startTx(false);

Post post = new Post("testPostTitle1", "testPostContent1");
post.save();
}
}

The jdbc connecte is OK. "But org.hibernate.PropertyAccessException:
could not get a field value by reflection getter of
models.Post.comments" exception again :(

Please help me. Thank you !!


btw post Post class define: (not modify)

@Entity
public class Post extends JPAModel {

private static final long serialVersionUID = 6181489305808812079L;

public String title;
public String content;
public Date postdate;

@OneToMany
public List<Comment> comments;

public Post(String title, String content) {
this.title = title;
this.content = content;
this.postdate = new Date();
}

public void addComment(String author, String comment) {
Comment c = new Comment(author, comment);
c.save();
comments.add(c);
}
}

lzy@china
2009/7/16

On Jul 16, 5:52 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Ok,
>
> I fixed things in the current trunk, revision 473.
> You should be able to run a DbUnit test case directly in the
> integrated TestRunner.
> Just use something like xxxxxTest as class name.
>
> If you really want to run a Play test case outside of the Play scope,
> things are more complicated.
> Consider using play.Invoker.invokeInThread() to run your code, as it
> will ensure that all Play stuff is correctly intialized.
>
> However using the integrated Test runner will be simpler.
>
> Guillaume.
>
> ...
>
> read more »

Guillaume Bort

unread,
Jul 16, 2009, 11:26:39 AM7/16/09
to play-fr...@googlegroups.com
Do you run the test using the integrated TestRunner or Eclipse ?

lzy.dev

unread,
Jul 16, 2009, 12:00:42 PM7/16/09
to play-framework
yes.

eclipes:
Version: 3.4.2
Build id: M20090211-1700

and junit4.


On Jul 16, 11:26 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Do you run the test using the integrated TestRunner or Eclipse ?
>
> ...
>
> read more »

Guillaume Bort

unread,
Jul 16, 2009, 12:43:14 PM7/16/09
to play-fr...@googlegroups.com
Can you send your app ?

Moreover why do you want to use DbUnit ? Play fixtures do the same
thing.

lzy.dev

unread,
Jul 16, 2009, 1:41:06 PM7/16/09
to play-framework
You are right. fixtures do the same. But dbunit more easy to create
test expecte data (export db data to dataset xml file). I feel.

"play_blog_sample.zip" has sent to you mail.

thks a lot. ;)

On Jul 17, 12:43 am, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Can you send your app ?
>
> Moreover why do you want to use DbUnit ? Play fixtures do the same  
> thing.
>
> ...
>
> read more »

Guillaume Bort

unread,
Jul 16, 2009, 4:05:24 PM7/16/09
to play-fr...@googlegroups.com
Ok.

I can't run your app because it uses Oracle.

However a couple of things :

- Your embeded version of slf4j conflicts with the one integrated with
the framework.
- Rename JpaModelTest as JpaModelTestBase or something that does not
end with *Test.
- Remove all the Play.init(...) and start(...) from the JpaModelTest
constructor. It is not needed.

To run the test :

- Run your app with 'play test'
- Open a browser to http://localhost:9000/@tests
- Enable the PostTest and click 'Start'

It should work.

lzy.dev

unread,
Jul 18, 2009, 8:37:40 AM7/18/09
to play-framework
OK. Thks.

At first I replaced slf4j version to integrated with the play!
framework.
But I got into 2 questions:

NO1. About use "play test"(http://localhost:9000/@test) to run my app
test case.

I renamed JpaModelTest as JpaModelTestBase. (not end with 'Test').
And as you say remove all the Play.init(...) and start(...) from the
JpaModelTest constructor.
Run my app test by "play test" browse "/@tests" and enable test and
start test.

But I got failed: (from application.log)

For request GET /@tests/unit.jpamodels.PostTest.class
Internal Server Error (500)

Template not found
The template TestRunner/results.html does not exist.

play.exceptions.TemplateNotFoundException: Template not found :
TestRunner/results.html
at play.templates.TemplateLoader.load(TemplateLoader.java:62)
at controllers.TestRunner.run(TestRunner.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at play.libs.Java.invokeStatic(Java.java:73)
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:113)
at play.server.HttpHandler$MinaInvocation.execute(HttpHandler.java:
399)
at play.Invoker$Invocation.doIt(Invoker.java:110)
at play.server.HttpHandler$MinaInvocation.doIt(HttpHandler.java:387)
at play.Invoker.invokeInThread(Invoker.java:44)
at play.server.HttpHandler.messageReceived(HttpHandler.java:80)
at Play!.HTTP Server(Mina)

But I can got success when run this sample test case:

import org.junit.Test;
import play.test.UnitTest;

public class SomeTest extends UnitTest {

@Test
public void fakeTest() {
assertEquals(2, 1+1); // A really important thing to test
}

}

Why? Is this a bug?


NO2. Use this method I can run DbUnit test case as you say "Allow to
run any class as JUnit test, even if it doesn't extend BasicTest". But
as we know test case is run by play! framework ('play test' and
browse /@test url). I want to ask how can I run unit test in play! by
JUNIT PLUGIN in ECLIPSE?? I think this method is more easy more fast
for developer.

Do you think so?


Thank you very much for help! thks.


On Jul 17, 4:05 am, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Ok.
>
> I can't run your app because it uses Oracle.
>
> However a couple of things :
>
> - Your embeded version of slf4j conflicts with the one integrated with
> the framework.
> - Rename JpaModelTest as JpaModelTestBase or something that does not
> end with *Test.
> - Remove all the Play.init(...) and start(...) from the JpaModelTest
> constructor. It is not needed.
>
> To run the test :
>
> - Run your app with 'play test'
> - Open a browser tohttp://localhost:9000/@tests
> - Enable the PostTest and click 'Start'
>
> It should work.
>
> ...
>
> read more »

lzy.dev

unread,
Jul 18, 2009, 9:38:52 AM7/18/09
to play-framework
OK.

According to my last post (above) I made same progress.
I fixed "Template not found. The template TestRunner/results.html does
not exist." exception problem. It's my mistake.

But I got a new question. I found out jpa model persistence delay when
I called jpa model object save method. Is play! has cache or something
else? For example:

Post post = new Post("testPostTitle1", "testPostContent1");
post.save(); // this will save record to LOCAL.POST table in oracle.

// need call same flush method??

IDataSet dbDataSet = getConnection().createDataSet();
ITable dbTable = dbDataSet.getTable("LOCAL.POST"); // but I can not
found any record saved by post.save() method.


Besides, I want to ask KEY QUESTION:
Use "play test"(browse http://localhost:9000/@test) method I can run
DbUnit test case as you say "Allow to
run any class as JUnit test, even if it doesn't extend BasicTest". But
as we know test case is run by play! framework. I want to ask how can
I run unit test in play! by JUNIT PLUGIN in ECLIPSE?? I think this
method is more easy more fast for developer.

Do you think so?

Thank you very much for help! thks.


> ...
>
> read more »

Guillaume Bort

unread,
Jul 18, 2009, 2:20:32 PM7/18/09
to play-fr...@googlegroups.com
You can't run your test cases outside of the Play environment. Play
manages a lot of things for you as any Java container (transaction
management, database connection pool, etc...). If you want to run them
in Eclipse you will basically have to start a Play server for each
test. That's why there is an integrated test runner.

About the database problem; When you call save(), it will not
necessary send the SQL statement to the database. You can have to call
explicitely flush() on the JPA manager.

Guillaume Bort

unread,
Jul 18, 2009, 2:33:01 PM7/18/09
to play-fr...@googlegroups.com
Another thing. As DBUnit will not share the same connection than
Hibernate, I think you will be faced to some transactions isolation
problems.

lzy.dev

unread,
Jul 19, 2009, 1:37:53 AM7/19/09
to play-framework
OK. thks

"Post.em().getTransaction().commit();"
can commit jpa thransaction.

As you say if I want to run test case in Eclipse I shoud have to start
a Play server for each test. I tried call Play.init, Play.start(),
play.db.jpa.JPAPlugin.startTx(false) method when test case start. But
I have failed:

"javax.persistence.PersistenceException:
org.hibernate.PropertyAccessException: could not get a field value by
reflection getter of models.Post.comments"

I said.

Thks.


On Jul 19, 2:33 am, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Another thing. As DBUnit will not share the same connection than  
> Hibernate, I think you will be faced to some transactions isolation  
> problems.
>
> On 18 juil. 09, at 15:38, "lzy.dev" <lzy....@gmail.com> wrote:
>
>
>
> > OK.
>
> > According to my last post (above) I made same progress.
> > I fixed "Template not found. The template TestRunner/results.html does
> > not exist." exception problem. It's my mistake.
>
> > But I got a new question. I found out jpa model persistence delay when
> > I called jpa model object save method. Is play! has cache or something
> > else? For example:
>
> >  Post post = new Post("testPostTitle1", "testPostContent1");
> >  post.save(); // this will save record to LOCAL.POST table in oracle.
>
> >  // need call same flush method??
>
> >  IDataSet dbDataSet = getConnection().createDataSet();
> >  ITable dbTable = dbDataSet.getTable("LOCAL.POST"); // but I can not
> > found any record saved by post.save() method.
>
> > Besides, I want to ask KEY QUESTION:
> > Use "play test"(browsehttp://localhost:9000/@test) method I can run
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages