Thursday, March 29, 2007

EasyMock and IllegalStateException

When writing a portlet and trying to unit test it, you definitly need somekind of mock implementation. I chose to use EasyMock 2.2 for that purpose because I think its easier to use. I will not explain nor defend this decision.

I have been using EasyMock for some time now, and every now and then I stumble over the problem IllegalStateException problem. Sometimes this is caused by not having 'replay'ed the mocks, but now its different. The stack look s like this:
java.lang.IllegalStateException: 2 matchers expected, 1 recorded.
at org.easymock.internal.ExpectedInvocation.createMissingMatchers(
at org.easymock.internal.ExpectedInvocation.(
at org.easymock.internal.ExpectedInvocation.(
at org.easymock.internal.RecordState.invoke(
at org.easymock.internal.MockInvocationHandler.invoke(
at org.easymock.internal.ObjectMethodsFilter.invoke(
This exception is thrown when executing request.setAttribute(MetaInfoPortlet.BEAN_ATTR, EasyMock.anyObject());.

So why is this excpetion being thrown at me? The message gives a hint, 2 matchers expected, recorded 1. So it lacks a matcher...

If I now change the code to request.setAttribute(EasyMock.matches(MetaInfoPortlet.BEAN_ATTR), EasyMock.anyObject());, it works fine. Now why is this? If I change the code to request.setAttribute(MetaInfoPortlet.BEAN_ATTR, "something");, it works as well, but now I get the following:
Unexpected method call setAttribute("bean", {test=Let's see if this tst value comes through?}):
setAttribute("bean", "something"): expected: 1, actual: 0
So it looks like the you either have to supply your mock with fixed values or you have to supply it with matchers, as it says in EasyMocks documentation: "If you would like to use matchers in a call, you have to specify matchers for all
arguments of the method call.

Bottom line: read the documentation.

Wednesday, March 21, 2007

JSTL and JSP Expression Language used in JSP 2.0

The release of the JSP 2.0 and servlet 2.4 specification has changed the way expressions are used within JSP pages.
From now on you can use expressions throughout the whole JSP page.
There appeared to be some limitations though, when mixing expressions with tags, you can only use expressions within tag attribute values.
So name is ${}. is perfectly legal, as well as href="${link.url}" />But ${attrName} is not.

When trying to figure this out, I stumbled over several issues before I got the desired output. First you must declare your application as a 2.4 webapp using the following declaration:

<web-app xmlns=""
xsi:schemaLocation=" web-app_2_4.xsd"
version="2.4" />

When using the JSTL taglibrary, you must be aware that the taglib url is different, jsp is added prior to jstl within the URL.

For 2.3 webapps you should use taglib <%@taglib uri="" prefix="c"%> while for 2.4 webapps you use <%@taglib uri="" prefix="c" %> />

Using Performancing when blogging

Today I figured out how the performancing firefox extension must be configured, so this will be the very first post I do from firefox.

I think this can become a very handy tool that eases posting to my blog!

Some interesting features are the use of a rich text editor.

See ya next time.