SeamTest and Conversations
cmoerz.seam.ny-central.org Feb 8, 2009 12:34 PMHi everyone,
I've run into trouble trying to get a more complex SeamTest to run. I want to test a seam application using inner FacesRequest objects. Those should be logging in, going to a page where a couple of database entries are listed (via a DataModel and a h:dataTable), select one of the entries and then edit that item and last but not least press
the save button.
Everything works fine up to the point where the item is about to be edited; that's where a conversation is started (via a Begin
annotated function) and everything goes wacko. The initial FacesRequest to the edit page, that starts the conversation works out fine, but when it comes to pressing
the save button (which is the next FacesRequest), it fails. First of all - yes, I do hand further the conversation id, but it seems like that conversation got stomped or something because in debug mode I saw that Manager.instance()... seems to believe that there's no conversation running anymore and the Component that's getting called/instantiated by the FacesRequest.invokeAction method has a different hashCode and therefore is another object than the one before. Theoretically this must not happen, because the component that handles that editing process is a stateful bean, so for the same request it must be the same component instance.
Is there anyone who can give me good example how this whole SeamTest thing works with a more complex test that goes over a couple of pages, starts and also ends a conversation? All the tests in the example directory are very brief and never ever have a conversation that goes over several FacesRequests. Is it possible that FacesRequest doesn't support a long
running conversation over several requests?
Let me show you some of my code I tried: That's an example, but in this case I tried to press
the cancel button, but even that fails with the exception that supposedly no transaction is running (duh, that's because I'm suddenly working on the wrong component...)
Here's the test:
public class EditCompanyTest extends HermesTest { @Test public void testShort() throws Exception { String id = login(); new FacesRequest( "/company.seam" ){ /*@SuppressWarnings("unchecked") @Override protected void renderResponse() { List<Company> listCompanies = (List<Company>) getValue("#{companyList.companies}"); assert null != listCompanies; assert listCompanies.size() > 0; assert !Manager.instance().isLongRunningConversation(); }*/ }.run(); new FacesRequest( "/company.seam") { @Override protected void updateModelValues() { Company company; List<Company> listCompanies = (List<Company>) getValue("#{companyList.companies}"); assert null != listCompanies; company = listCompanies.get( 0 ); assert null != company; setValue( "#{company}", company ); } @Override protected void invokeApplication() { invokeAction( "#{companyList.selectCompany(company)}" ); assert null != getValue( "#{companyList.selCompany}" ); } @Override protected void renderResponse() { } }.run(); id = new FacesRequest( "/company/view.seam" ) { @Override protected void invokeApplication() { invokeAction( "#{companyEdit.editCompany( selCompany )}" ); } @Override protected void renderResponse() { assert Manager.instance().isLongRunningConversation(); } }.run();
Up to here, everything works fine - id is retuned as 10
.
new FacesRequest( "/company/edit.seam", id ) { @Override protected void updateModelValues() throws Exception { CompanyEdit cmpEdit = (CompanyEdit) getValue( "#{companyEdit}" ); assert Manager.instance().isLongRunningConversation(); setValue( "#{companyEdit.companyObj.name}", "Renamed Company" ); setValue( "#{companyEdit.companyObj.address.street}", "Street value" ); } @Override protected void invokeApplication() { invokeAction( "#{companyEdit.cancel}" ); } }.run();
And now it happened: when the FacesRequest wants to invoke cancel, it fails with a nice, huge exception.
new FacesRequest( "/company/view.seam" ) { @Override protected void renderResponse() { Company company; assert !Manager.instance().isLongRunningConversation(); company = (Company) getValue( "#{companyList.selCompany}" ); assert company.getName().equals( "Test Company AG" ); } }.run(); } }
If anyone can give me some input or point me to a more in-depth resource for SeamTest/FacesRequest and Seam testing in general - or even better, point me at some mistake I made, I'd be very thankful.
By the way - can anyone please explain to me, what the difference is between FacesRequest and NonFacesRequest? What's the difference between those two? I mean, I understand that FacesRequest emulates the web
access to a faces page, but what's the NonFacesRequest good for?
thanks in advance
chris