Friday, May 6, 2011

Mocking jndi for junit tests

SimpleNamingContextBuilder was the perfect match for me when i wanted to mock some jndi-references for my appctx in my junit-tests

@BeforeClass
    public static void mockJndi() throws IllegalStateException, NamingException {
        SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
        builder.bind("java.naming.factory.initial", new org.jnp.interfaces.NamingContextFactory());
        builder.bind("ConnectionFactory", new QueueConnectionFactoryBean());
        builder.bind("queue/someFileQueue", new ActiveMQQueue());
        builder.activate();
    }.

Thanks to http://formatmysourcecode.blogspot.com/ :)