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