Joshua Java

Providing conversationId for testing in Seam

Posted 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();
	}
}
  1. 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.
  2. You then put the id that is generated on previous line in a List of String.
  3. 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.
  4. Now you can start the conversation with the conversation component instance and invoke your conversational method.🙂
  5. Don’t forget to end the conversation😉

IMHO Seam is really cool indeed.

Tags:

1 Response to "Providing conversationId for testing in Seam"

can you help me, tell me the how can i import the example from seam distribution so i can edit it in my eclipse?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: