Providing conversationId for testing in Seam
Posted January 25, 2008
on: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(); } }
- So first you need to create a conversation id with the ConversationIdGenerator component. Now there are two ways to create it, the remarked line is the first way but I like the second way better. If you take a look at the code, that’s how Seam create the conversation id as in real apps, though as I said before you can just create your own id which Seam don’t care about.
- You then put the id that is generated on previous line in a List of String.
- Now you set the conversation id and the conversation id stack and pass it to Seam conversation manager component as it will be used for the conversation.
- Now you can start the conversation with the conversation component instance and invoke your conversational method. π
- Don’t forget to end the conversation π
IMHO Seam is really cool indeed.
1 | princessay
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?