Wednesday 16 June 2010

Spring - JUnit - Mocking JNDI

JNDI You are awful..

Oh ok, not that kind of mocking. But seriously if you are doing this in a spring bean:

@Resource
private String rootURL

Then you can use Spring to define a a bean to fill it in, then all is well. But what if you are doing:

@Resource(mappedName="rootURL")
private String rootURL

Well then you are most likely using something like a deployment descriptor (e.g. web.xml) to define the environment variable e.g.

    <env-entry>
        <description>Root URL for a blog</description>
        <env-entry-name>rootURL</env-entry-name>
        <env-entry-type>java.lang.String</env-entry-type>
        <env-entry-value>http://www.blogger.com</env-entry-value>
    </env-entry>


But what happens when you try to use JUnit, probably nothing bad, but what if you are using Spring to run your JUnit? Then you will most likely get this kind of error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'blogDAO':
Injection of resource fields failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'rootURL' defined in JNDI environment:
JNDI lookup failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

This is down there not being the JNDI eniroment you were expecting, so how can you mock one up? Buy adding this to your JUnit test:

public static void buildJndi() {

        try {

            SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();

            String rootURL = "http://www.blogger.com";
            builder.bind("rootURL", rootURL);

        } catch (NamingException e) {

        }

    }

and calling it in the unit tests constructor.

For more information checkout:
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/mock/jndi/SimpleNamingContextBuilder.html

Thursday 10 June 2010

JAX-WS 2.2 and Tomcat

I wanted to consume a web-service within a webapp on Tomcat, after being so used to using the Fish I deployed with thought to libraries and came up with this error:

NoSuchMethodError WebFault.messageName


After much hunting around I found that Netbeans had imported the Metro 2.0 library, so I changed this to JAX-WS 2.2 (jaxb-api.jar, jaxws-api.jar, jaxws-tools.jar) and everything is now fine..

Res:
http://weblogs.java.net/blog/ramapulavarthi/archive/2009/04/tip_for_using_j.html

Tuesday 8 June 2010

Accessing Spring Beans in Servlet

Sometimes you just have to do the do in a servlet. Here is a nice cheeky little way of getting to the applciation context:

WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext())

Then from there you can use getBean() etc...

Ref: http://forum.springsource.org/showthread.php?t=18833