implements Elegance {

// Elwyn Malethan's musings on software development, mountain biking and general navel–gazing...

Articles tagged with 'junit'

Unit testing with Commons HttpClient

I‘ve been adding some test coverage to some existing code today. This code under test uses Commons HTTP Client to fetch data from an external resource. I ran into problems with mocking/stubbing the response to the HTTP requests.

I am not the only one who has faced problems with this Http Client. In that article, Florin provides a good solution using a concrete subclass of HttpClient in order to mock it‘s behaviour. His article was very useful in helping me formulate my approach.

I use JMock for dynamic mock objects and so I was keen not to have to mix of JMock-based mocks and concrete mock objects. So my approach is to use JMock‘s custom actions to provide an implementation that populates the right values in the right way. Although, I had to use some reflective cheating to set the necessary values.

private static Action willRespondWith(final int code, final String status, final String body) {
    return new CustomAction("will respond with status: " + code +  ", and contents: " + body) {
        @Override
        public Object invoke(Invocation invocation) throws Throwable {
            final Object method = invocation.getParameter(0);

            final Field statusLine = HttpMethodBase.class.getDeclaredField("statusLine");
            statusLine.setAccessible(true);
            statusLine.set(method, new StatusLine("HTTP/1.0 " + code + " " + status)); //Although, 'OK' could be anything 

            final Method setResponseStream = HttpMethodBase.class.getDeclaredMethod("setResponseStream", InputStream.class);
            setResponseStream.setAccessible(true);
            setResponseStream.invoke(method, new ByteArrayInputStream(body.getBytes("UTF-8")));

            return code;
        }
    };
}

This is intended to be used as follows:

@Test
public void testSomeHttpStuff() throws Exception {

    jmock.checking(new Expectations() {{
        allowing(client).executeMethod(with(any(HttpMethod.class)));
        will(willRespondWith(200, "OK", "{some: 'value'}"));
    }});
        
    sendHttpRequest("http://some.server/some/resource.json");
        
}

Now I can simulate all sorts of responses from the external resource.

First published on Apr 26, 2010. Last updated on: Apr 26, 2010.

Testing context for UrlRewrite and JUnit4

Absolutely ages ago, I found this great article on JUnit testing for UrlRewrite by Sujit Pal. It was very useful as at the time as I was looking for a way of testing that my usage of UrlRewrite was working correctly without having a servlet container running. Together with my contribution, both inbound and outbound rules are covered.

The solution is great for JUnit 3 but not really in the spirit of JUnit 4, where the test cases aren't necessarily part of an inheritance hierarchy, as this article demonstrates.

I had cause to test some rewrites again recently. However, I‘ve moved over to JUnit4 as a preference these days, so I decided to refresh Sujit‘s solution, decouple it from TestCase.

package some.pakage.name;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.tuckey.web.MockRequest;
import org.tuckey.web.MockResponse;
import org.tuckey.web.filters.urlrewrite.Conf;
import org.tuckey.web.filters.urlrewrite.RewrittenUrl;
import org.tuckey.web.filters.urlrewrite.UrlRewriteWrappedResponse;
import org.tuckey.web.filters.urlrewrite.UrlRewriter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * <p>A context for testing UrlRewrite rules for JUnit4</p>
 */
public class UrlRewriteTesting {
    //----------------------------------------------------------------------- Instance Properties

    protected Conf conf;
    protected UrlRewriter rewriter;

    //----------------------------------------------------------------------- Constructors

    public UrlRewriteTesting(Conf conf) {
        org.tuckey.web.filters.urlrewrite.utils.Log.setLevel("DEBUG");
        this.conf = conf;
        rewriter = new UrlRewriter(conf);
    }

    public UrlRewriteTesting(String pathToCfg, String confFile) throws FileNotFoundException {
        this(new Conf(new FileInputStream(pathToCfg + "/" + confFile), confFile));
    }

    //----------------------------------------------------------------------- Instance Methods

    /**
     * Assertion to rewrite the URL using the UrlRewriteFilter and verify
     * that fromUrl is rewritten to toUrl using rewriting rules in conf.
     *
     * @param fromUrl the URL to be rewritten from.
     * @param toUrl   the URL to be rewritten to.
     */
    public void assertInboundRewrite(String fromUrl, String toUrl) {
        RewrittenUrl rewrittenUrl = rewriter.processRequest(new MockRequest(fromUrl), new MockResponse());

        assertNotNull("Could not rewrite URL from:" + fromUrl + " to:" + toUrl, rewrittenUrl);

        assertEquals("Rewrite from:" + fromUrl + " to:" + toUrl + " did not succeed", toUrl, rewrittenUrl.getTarget());
    }

    /**
     * Tests that an outbound url is processed correctly by the outbound rules
     *
     * @param fromUrl the url as you would be writing it in your JSPs
     * @param toUrl what you expect it to be rewritten to when rendered
     */
    public void assertOutboundRewrite(String fromUrl, String toUrl) {
        UrlRewriteWrappedResponse wrappedResponse =
                new UrlRewriteWrappedResponse(new MockResponse(), new MockRequest(fromUrl), new UrlRewriter(conf));

        assertEquals("Rewrite from:" + fromUrl + " to:" + toUrl + " did not succeed", toUrl, wrappedResponse.encodeURL(fromUrl));
    }

}

Usage

Below is an example of how SeemoreJ's rewrite rules in urlrewrite.xml for this site might look for the home page. At the time of writing, this site does not use UrlRewrite, so it's entirely hypothetical.

<!-- Index/Home -->
    <rule>
        <from>^/$</from>
        <to>/seemore?sjController=blog&amp;sjAction=home&amp;sjFormat=html</to>
    </rule>
    <rule>
        <from>^/index.(xml|html)$</from>
        <to>/seemore?sjController=blog&amp;sjAction=home&amp;sjFormat=$1</to>
    </rule>

    <outbound-rule>
        <from>/seemore\?sjController=blog&amp;sjAction=home&amp;sjFormat=(xml|html)</from>
        <to>/index.$1</to>
    </outbound-rule>

I might be making numerous minor or major changes to this file and I want the unit tests to defend against unwanted side-effects. So the test case below will (hopefully) make sure I don‘t mess things up.

package com.malethan.blog.urlrewrite;

import some.pakage.name.UrlRewriteTesting;
import org.junit.Test;

import java.io.FileNotFoundException;

public class UrlRewriteRulesTest {
    //----------------------------------------------------------------------- Instance Properties

    UrlRewriteTesting context;

    //----------------------------------------------------------------------- Constructors

    public UrlRewriteRulesTest() throws FileNotFoundException {
        context = new UrlRewriteTesting(System.getProperty("basedir") +"/src/main/webapp/WEB-INF", "urlrewrite.xml");
    }

    //----------------------------------------------------------------------- Tests

    @Test
    public void testIndexInbound() {
        context.assertInboundRewrite("/", "/seemore?sjController=blog&sjAction=home&sjFormat=html");
        context.assertInboundRewrite("/index.html", "/seemore?sjController=blog&sjAction=home&sjFormat=html");
        context.assertInboundRewrite("/index.xml", "/seemore?sjController=blog&sjAction=home&sjFormat=xml");
    }

    @Test
    public void testIndexOutbound() {
        context.assertOutboundRewrite("/seemore?sjController=blog&sjAction=home&sjFormat=html", "/index.html");
        context.assertOutboundRewrite("/seemore?sjController=blog&sjAction=home&sjFormat=xml", "/index.xml");
    }
}

First published on Dec 20, 2008. Last updated on: Dec 30, 2009.

 
People I like
Other sites