Sebastien,
Stripersist is a very thin layer on top of JPA. A very useful layer
mind you that integrates very well with Stripes.
If you say that your code is working then I suspect its how you are
dealing with things in your tests... not necessarily the
MockServletContext IMO.
If I look at your tests I see you call the following at the top of the
tests:
User user = createUser();
Without seeing your code I assume this creates a mock object OR loads a
canned object from the DB.
If you want to update it then you will need to do a "merge" on this
object vs. doing a "persist" (which is fine for a new object).
If you want to delete it then you will need to do a "merge" and then a
"remove" (of course this isn't very efficient and there are other ways
but that's what you get for using the basic capabilities of an ORM i.e.
you could write some native sql query or something to do a straight
delete by PK).
If you don't do the "merge" first then the object can not re-attach to a
persistent context.
If this doesn't help then please provide more code for:
- User user = createUser();
- getDao().save(getUser());
- getDao().delete(getUser ());
HTH.
--Nikolaos
Sebastien Belin wrote:
Thanks for your answer Nikolaos,
Yeah the actionBean is beend tested without the Mock/Test framwerok.
In fact the code I use for the ActionBean is re-used from another
application which is working perfectly but didn't have any action tests.
I understand what you're saying about the object being detached but I
thought that was one of the point of using Stripersist, and I can't
really see why it does work in normal mode and not in test mode.
I found a lot of article in google talking about ORM and detached
object with JPA/Hibernate but nothing for MockServletContext with
Stripersist that why I posted to the email list.
Anyway, thank you for your help.
Sebastien
*From:* Nikolaos Giannopoulos [mailto:nikol...@brightminds.org]
*Sent:* 07 April 2011 19:32
*To:* Stripes Users List
*Subject:* Re: [Stripes-users] Stripes testing with MockServletContext
and Stripersist
Sebastien,
Have you tested your ActionBean directly without using Mock / Test
framework? I'm going to guess that it won't work there either.
Because ORM's have the notion of persistence context... if you load an
object in one transaction and later try to persist it the object in
another transaction the object is considered detached and needs to be
re-attached to a persistence context... say for example using merge
vs. persist.
In any event, if you google your error you should find lots of
information on this topic.
This is a common issue for update or delete w/ JPA and / or Hibernate.
--Nikolaos
Sebastien Belin wrote:
Hi All,
I've started a new simple project using stripes and stripersist/hibernate.
As the basic development is finished I wanted to add some testing.
Folowing the example given in
http://www.stripesframework.org/display/stripes/Unit+Testing I've used
MockServletContext.
I want to test my UserActionBean. This class has simple handler method
such as list(), view(), cancel(), create(), edit(), save(), delete()
and update();
I also have a getter and setter for the User entity.
The update() method handler is as simple as:
public Resolution update() {
getDao().save(getUser());
getDao().commit();
return new RedirectResolution(this.getClass(),
"view").addParameter("user", getUser());
}
The delete() method handler:
public Resolution delete() {
getDao().delete(getUser ());
getDao().commit();
return new RedirectResolution(this.getClass());
}
I've writen a test class UserActionBeanTest and all test run correctly
(list, view, create, save ...) but the update and delete one.
@Test
public void testDelete() throws Exception {
User user = createUser();
MockRoundtrip mockRoundtrip = new MockRoundtrip(getServletContext(),
UserActionBean.class);
mockRoundtrip.addParameter("user", String.valueOf(user.getId()));
mockRoundtrip.execute("delete");
String redirectUrl = (new
RedirectResolution(UserActionBean.class)).getUrl(Locale.getDefault());
verifyDestination(mockRoundtrip, redirectUrl);
}
@Test
public void testUpdate() throws Exception {
User user = createUser();
MockRoundtrip mockRoundtrip = new MockRoundtrip(getServletContext(),
UserActionBean.class);
mockRoundtrip.addParameter("user", String.valueOf(user.getId()));
mockRoundtrip.addParameter("user.name", "newName");
mockRoundtrip.execute("update");
RedirectResolution redirectResolution = new
RedirectResolution(UserActionBean.class, "view").addParameter("user",
user);
String redirectUrl = redirectResolution.getUrl(Locale.getDefault());
verifyDestination(mockRoundtrip, redirectUrl);
assertUserUpdated(user);
}
When I run them I received some error message saying that the object
user is detached.
For Update:
javax.persistence.PersistenceException:
org.hibernate.PersistentObjectException: detached entity passed to
persist: com.package.model.User
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1214)
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147)
at
org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1153)
at
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:678)
at
com.package.dao.jpa.JpaBaseDao.save(JpaBaseDao.java:121)
at
com.package.action.admin.EntityAdminBean.update(EntityAdminBean.java:82)
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
net.sourceforge.stripes.controller.DispatcherHelper$6.intercept(DispatcherHelper.java:467)
at
net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
at
net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
at
net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
at
net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
at
net.sourceforge.stripes.controller.DispatcherHelper.invokeEventHandler(DispatcherHelper.java:465)
at
net.sourceforge.stripes.controller.DispatcherServlet.invokeEventHandler(DispatcherServlet.java:278)
at
net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:160)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at
net.sourceforge.stripes.mock.MockFilterChain.doFilter(MockFilterChain.java:66)
at
net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
... 31 more
Caused by: org.hibernate.PersistentObjectException: detached entity
passed to persist: com.package.model.User
at
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:127)
at
org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
at
org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
at
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
at
org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
at
org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
For Delete:
java.lang.IllegalArgumentException: Removing a detached instance
com.package.model.User#95
at
org.hibernate.ejb.event.EJB3DeleteEventListener.performDetachedEntityDeletionCheck(EJB3DeleteEventListener.java:65)
at
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:107)
at
org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:73)
at
org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:956)
at
org.hibernate.impl.SessionImpl.delete(SessionImpl.java:934)
at
org.hibernate.ejb.AbstractEntityManagerImpl.remove(AbstractEntityManagerImpl.java:702)
at
com.package.dao.jpa.JpaBaseDao.delete(JpaBaseDao.java:70)
at
com.package.action.admin.EntityAdminBean.delete(EntityAdminBean.java:68)
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
net.sourceforge.stripes.controller.DispatcherHelper$6.intercept(DispatcherHelper.java:467)
at
net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:158)
at
net.sourceforge.stripes.controller.BeforeAfterMethodInterceptor.intercept(BeforeAfterMethodInterceptor.java:113)
at
net.sourceforge.stripes.controller.ExecutionContext.proceed(ExecutionContext.java:155)
at
net.sourceforge.stripes.controller.ExecutionContext.wrap(ExecutionContext.java:74)
at
net.sourceforge.stripes.controller.DispatcherHelper.invokeEventHandler(DispatcherHelper.java:465)
at
net.sourceforge.stripes.controller.DispatcherServlet.invokeEventHandler(DispatcherServlet.java:278)
at
net.sourceforge.stripes.controller.DispatcherServlet.service(DispatcherServlet.java:160)
at
javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at
net.sourceforge.stripes.mock.MockFilterChain.doFilter(MockFilterChain.java:66)
at
net.sourceforge.stripes.controller.StripesFilter.doFilter(StripesFilter.java:247)
I really can't work out what's wrong and why the object is detached
when using MockServletContext.
If any one could help it would be much appreciated.
Don't hesitate to ask if you need more info.
Thanks.
Seb
------------------------------------------------------------------------
------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net <mailto:Stripes-users@lists.sourceforge.net>
https://lists.sourceforge.net/lists/listinfo/stripes-users
--
Nikolaos Giannopoulos
Director, Information Technology
BrightMinds Software Inc.
e. nikol...@brightminds.org <mailto:nikol...@brightminds.org>
w. www.brightminds.org <http://www.brightminds.org>
t. 1.613.822.1700
c. 1.613.797.0036
f. 1.613.822.1915
------------------------------------------------------------------------
------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users
--
Nikolaos Giannopoulos
Director, Information Technology
BrightMinds Software Inc.
e. nikol...@brightminds.org
w. www.brightminds.org
t. 1.613.822.1700
c. 1.613.797.0036
f. 1.613.822.1915
------------------------------------------------------------------------------
Xperia(TM) PLAY
It's a major breakthrough. An authentic gaming
smartphone on the nation's most reliable network.
And it wants your games.
http://p.sf.net/sfu/verizon-sfdev
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users