I am happy to announce the launch of a new library called AM or Assert-Mocks for unit-testing Java servlet API code. The library aims to make it a lot easier and faster to test the servlet API code, including and not limited to testing of servlets, servlet filters and tag libraries. It helps remove the need of using Mockito framework for setting up expectations.


The following two examples should help you get started easily. More examples are being added to the Github:sangupta/am repository.

Testing a simple JSP tag


    // the class implementing custom tag

    // the expected String response

    // set the values before invocation
    new GenericConsumer<MyCustomJSPTag>() {

        // method argument of same type as the first param
        public boolean consume(MyCustomJSPTag tag) {
            // set the properties of the tag

            // and so on...

            // return either true or false
            // it does not matter in this case
            return true;


Testing a Servlet Filter

// obtain an instance of the filter
MyCustomFilter filter = AmServletFilterTestHelper.getFilterForUnitTesting(MyCustomFilter.class);

// create request and response objects as filter will need them
// the AmHttpServletRequest.getDefault() method returns a request for a server
// deployed on localhost on port 80, and being hit with same machine where
// the servlet context is `context` and the path is `/home.html`
AmHttpServletRequest request = AmHttpServletRequest.getDefault("home.html");

// the response object to which filter will write
AmHttpServletResponse response = new AmHttpServletResponse();

// filter invocation
AmServletFilterTestHelper.assertFilterChainInvoked(filter, request, response);

// assert what was set in response
Assert.assertEquals(200, response.getStatusCode());
Assert.assertEquals("myCustomHeaderValue", response.getHeader("X-Custom-Header"));

Source & Downloads

The source code for the library is available at Github:sangupta/am

The library is available at the following Maven coordinates:


Happy Hacking!