Skip to content
Go to file

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Assertions & Mocks (AM)

Travis Coveralls license Maven Central

am makes it super-easy to unit-test Java Servlet API code by providing various mock implementations and helper classes.

The library is tested on the following JDK versions:

  • Oracle JDK 7, 8, 9, 11, 13
  • Open JDK 7, 8, 9, 10, 11, 12, 13


The library provides convenience utility methods for testing for:

  • Testing JSP tags - short-cut way
  • Testing JSP tags - full-fledged way
  • Testing of Servlet filters

Testing a simple JSP tag

    // the class implementing custom tag
    // the expected String response

    // set the values before invocation
    new GenericConsumer<MyCustomJSPTag>() {
        public boolean consume(MyCustomJSPTag tag) {
            // set the properties of the tag
            // and so on...
            return true;


Testing a custom JSP tag

// let's say we are testing a Base64Tag class
// that writes the base-64 encoded/decoded value

// create a tag-instance that is wired for unit testing
Base64Tag tag = AmTagLibTestHelper.getTagForUnitTesting(Base64Tag.class);

// set any property that is required by the tag

// execute the tag - this method only converts checked exceptions to runtime ones

String expectedEncoded = "... the actual base64 encoded value ...";

// the method converts any value written to JspWriter into String in null-safe fashion
String actualEncoded = AmTagLibTestHelper.getJspWriterString(tag);

// compare the values
Assert.assertEquals(expectedEncoded, actualEncoded);

Testing a Servlet Filter

The code below is used to test MyCustomFilter by passing the relevant wired ServletRequest and ServletResponse objects making sure that the internal FilterChain supplied is invoked.

public void testFilter() {
  // 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`
  MockHttpServletRequest request = MockHttpServletRequest.getDefault("home.html");

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

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

A similar method AmServletFilterTestHelper.assertFilterChainNotInvoked is available that checks that during filter execution, the FilterChain was not invoked.

Mock availability

Dummy or mock implementations are available for the following Servlet API classes:

Along with are available the following support classes to aid in testing:


  • Current Snapshot

  • Version 1.1.0 (14 May 2019)

    • Updated field modifiers to public to allow access while assertions
  • Version 1.0.0 (21 Dec 2016)

    • Initial release

Release Downloads

The latest release can be downloaded from Maven Central:


Snapshot Downloads

The library is currently under infancy and being updated continually, and thus has not been released. However, for development purposes you may use JitPack to pull in the latest snapshot build.

For JitPack, add the following repository to Maven,


And then, add the dependency as,



For transparency and insight into our release cycle, and for striving to maintain backward compatibility, am will be maintained under the Semantic Versioning guidelines as much as possible.

Releases will be numbered with the follow format:


And constructed with the following guidelines:

  • Breaking backward compatibility bumps the major
  • New additions without breaking backward compatibility bumps the minor
  • Bug fixes and misc changes bump the patch

For more information on SemVer, please visit


am: Assert-Mocks for unit-testing Java servlet API code
Copyright (c) 2016-2020, Sandeep Gupta

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.