Posted by: Joshua on: January 25, 2008
I think Seam is really great for handling conversational context, but recently I faced a problems when I wanted to unit test my action method that is annotated with @Conversational. Now if you annotate your action method with @Conversational, this means Seam assume you are in a conversation to access that method, which means you have to have a conversation id to pass to Seam. Now the problem is how do you provide these conversation id for testing in Seam? Luckily everything is very simple in Seam and I can pass a dummy id and Seam don’t really care if it is as real as in real apps or not. But I wanted to make it as real as it is in real apps, and here it goes:
Assuming you have created your action with @Conversational method as such:
@Name("applicationForm")
public class ApplicationForm implements Serializable{
@Logger Log log;
@Conversational
public void save(){
log.info("Saving...");
}
}
Here is the in testing code which I assume you’re already familiar with, if you don’t you can take a look Seam docs regarding testing.
public class ApplicationTest extends SeamTest {
private String cid;
private List<String> cidStack;
private static LogProvider log = Logging
.getLogProvider(ApplicationTest.class);
@Test
public void testApplicationForm() throws Exception {
new ComponentTest() {
protected void testComponents() throws Exception {
// cid = (String) invokeMethod("#{ConversationIdGenerator.getNextId}");
// log.info("cid= " + cid);
cid = ConversationIdGenerator.instance().getNextId();
log.info("cid= " + cid);
cidStack = new ArrayList<String>();
cidStack.add(cid);
Manager.instance().setCurrentConversationId(cid);
Manager.instance().setCurrentConversationIdStack(cidStack);
Conversation.instance().begin();
invokeMethod("#{applicationForm.save}");
Conversation.instance().end();
}
}.run();
}
}
IMHO Seam is really cool indeed.
May 15, 2009 at 1:32 pm
can you help me, tell me the how can i import the example from seam distribution so i can edit it in my eclipse?