{"id":15297529,"url":"https://github.com/sangupta/am","last_synced_at":"2026-01-05T19:09:40.403Z","repository":{"id":55909126,"uuid":"76486922","full_name":"sangupta/am","owner":"sangupta","description":"Assert-Mocks for unit-testing Java servlet API code","archived":false,"fork":false,"pushed_at":"2020-12-08T00:55:18.000Z","size":141,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-30T12:14:38.168Z","etag":null,"topics":["httpservletrequest","httpservletresponse","java","jsp-tag","junit","mock-implementations","servlet-api","servlet-filter","unit-testing"],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sangupta.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-12-14T18:40:35.000Z","updated_at":"2021-09-21T18:48:02.000Z","dependencies_parsed_at":"2022-08-15T09:10:42.066Z","dependency_job_id":null,"html_url":"https://github.com/sangupta/am","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangupta%2Fam","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangupta%2Fam/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangupta%2Fam/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sangupta%2Fam/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sangupta","download_url":"https://codeload.github.com/sangupta/am/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245467615,"owners_count":20620216,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["httpservletrequest","httpservletresponse","java","jsp-tag","junit","mock-implementations","servlet-api","servlet-filter","unit-testing"],"created_at":"2024-09-30T19:18:11.051Z","updated_at":"2026-01-05T19:09:40.318Z","avatar_url":"https://github.com/sangupta.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Assertions \u0026 Mocks (AM)\n\n[![Travis](https://img.shields.io/travis/sangupta/am.svg)](https://travis-ci.org/sangupta/am)\n[![Coveralls](https://img.shields.io/coveralls/sangupta/am.svg)](https://coveralls.io/github/sangupta/am)\n[![license](https://img.shields.io/github/license/sangupta/am.svg)](https://choosealicense.com/licenses/apache-2.0/)\n[![Maven Central](https://img.shields.io/maven-central/v/com.sangupta/am.svg)](https://search.maven.org/search?q=g:com.sangupta%20AND%20a:am\u0026core=gav)\n\n`am` makes it super-easy to unit-test Java Servlet API code by providing various mock\nimplementations and helper classes.\n\nThe library is tested on the following JDK versions:\n\n* Oracle JDK 7, 8, 9, 11, 13\n* Open JDK 7, 8, 9, 10, 11, 12, 13\n\n# Usage\n\nThe library provides convenience utility methods for testing for:\n\n* Testing JSP tags - short-cut way\n* Testing JSP tags - full-fledged way\n* Testing of Servlet filters\n\n## Testing a simple JSP tag\n\n```java\nAmTagLibTestHelper.testTagOutput(\n    // the class implementing custom tag\n    MyCustomJSPTag.class,\n    \n    // the expected String response\n    expectedOutputWrittenToJspWriter,\n\n    // set the values before invocation\n    new GenericConsumer\u003cMyCustomJSPTag\u003e() {\n\t\n        public boolean consume(MyCustomJSPTag tag) {\n            // set the properties of the tag\n            tag.setProperty1(prop1);\n            tag.setProperty2(prop2);\n\t\n            // and so on...\n\t\t\t\n            return true;\n        }\n    }\n\n);\n```\n\n### Testing a custom JSP tag\n\n```java\n// let's say we are testing a Base64Tag class\n// that writes the base-64 encoded/decoded value\n\n// create a tag-instance that is wired for unit testing\nBase64Tag tag = AmTagLibTestHelper.getTagForUnitTesting(Base64Tag.class);\n\n// set any property that is required by the tag\ntag.setEncode(value);\n\n// execute the tag - this method only converts checked exceptions to runtime ones\nAmTagLibTestHelper.doTag(tag);\n\nString expectedEncoded = \"... the actual base64 encoded value ...\";\n\n// the method converts any value written to JspWriter into String in null-safe fashion\nString actualEncoded = AmTagLibTestHelper.getJspWriterString(tag);\n\n// compare the values\nAssert.assertEquals(expectedEncoded, actualEncoded);\n```\n\n### Testing a Servlet Filter\n\nThe code below is used to test `MyCustomFilter` by passing the relevant wired `ServletRequest`\nand `ServletResponse` objects making sure that the internal `FilterChain` supplied is invoked.\n\n```java\n@Test\npublic void testFilter() {\n  // obtain an instance of the filter\n  MyCustomFilter filter = AmServletFilterTestHelper.getFilterForUnitTesting(MyCustomFilter.class);\n\n  // create request and response objects as filter will need them\n  // the AmHttpServletRequest.getDefault() method returns a request for a server\n  // deployed on localhost on port 80, and being hit with same machine where\n  // the servlet context is `context` and the path is `/home.html`\n  MockHttpServletRequest request = MockHttpServletRequest.getDefault(\"home.html\");\n\n  // the response object to which filter will write\n  MockHttpServletResponse response = new MockHttpServletResponse();\n\n  // filter invocation\n  AmServletFilterTestHelper.assertFilterChainInvoked(filter, request, response);\n}\n```\n\nA similar method `AmServletFilterTestHelper.assertFilterChainNotInvoked` is available that\nchecks that during filter execution, the `FilterChain` was not invoked.\n\n## Mock availability\n\nDummy or mock implementations are available for the following Servlet API classes:\n\n* [FilterChain][1]\n* [HttpServletRequest][2]\n* [HttpServletResponse][3]\n* [HttpSession][4]\n* [JspContext][5]\n* [JspWriter][6]\n* [PageContext][7]\n* [Principal][8]\n* [RequestDispatcher][9]\n* [ServletRequest][10]\n* [ServletResponse][11]\n\nAlong with are available the following support classes to aid in testing:\n\n* [MockExceptionHandler][12] - Exception handler to be used in conjunction with [MockPageContext][7]\n* [MockFowwardOrIncludeHandler][13] - handles forward/include calls in [MockPageContext][7]\n* [MockUrlEncoder][14] - a simple URL encoder to help in encoding URLs  in [MockHttpServletResponse][3]\n* [ByteArrayServletInputStream][15] - can be used with the [MockServletRequest][10] to provide input\n* [ByteArrayServletOutputStream][16] - can be used with [MockServletResponse][11] to capture output\n\n## Changelog\n\n* Current Snapshot\n  * Renamed classes to use `Mock` when they are mocking a JRE class\n  * Headers are now case-sensitive in [HttpServletRequest][2] and [HttpServletResponse][3]\n\n* Version 1.1.0 (14 May 2019)\n  * Updated field modifiers to `public` to allow access while assertions\n  \n* Version 1.0.0 (21 Dec 2016)\n  * Initial release\n\n## Release Downloads\n\nThe latest release can be downloaded from Maven Central:\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.sangupta\u003c/groupId\u003e\n    \u003cartifactId\u003eam\u003c/artifactId\u003e\n    \u003cversion\u003e1.1.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Snapshot Downloads\n\nThe library is currently under infancy and being updated continually, and thus has not\nbeen released. However, for development purposes you may use [JitPack](https://jitpack.io)\nto pull in the latest snapshot build.\n\nFor **JitPack**, add the following `repository` to Maven,\n\n```xml\n\u003crepository\u003e\n    \u003cid\u003ejitpack.io\u003c/id\u003e\n    \u003curl\u003ehttps://jitpack.io\u003c/url\u003e\n\u003c/repository\u003e\n```\n\nAnd then, add the dependency as,\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.github.sangupta\u003c/groupId\u003e\n    \u003cartifactId\u003eam\u003c/artifactId\u003e\n    \u003cversion\u003e-SNAPSHOT\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Versioning\n\nFor transparency and insight into our release cycle, and for striving to maintain backward compatibility, \n`am` will be maintained under the Semantic Versioning guidelines as much as possible.\n\nReleases will be numbered with the follow format:\n\n`\u003cmajor\u003e.\u003cminor\u003e.\u003cpatch\u003e`\n\nAnd constructed with the following guidelines:\n\n* Breaking backward compatibility bumps the major\n* New additions without breaking backward compatibility bumps the minor\n* Bug fixes and misc changes bump the patch\n\nFor more information on SemVer, please visit http://semver.org/.\n\n## License\n\t\n```\nam: Assert-Mocks for unit-testing Java servlet API code\nCopyright (c) 2016-2020, Sandeep Gupta\n\nhttps://sangupta.com/projects/am\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n\thttp://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n```\n\n\n[1]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockFilterChain.java\n[2]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockHttpServletRequest.java\n[3]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockHttpServletResponse.java\n[4]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockHttpSession.java\n[5]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockJspContext.java\n[6]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockJspWriter.java\n[7]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockPageContext.java\n[8]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockPrincipal.java\n[9]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockRequestDispatcher.java\n[10]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockServletRequest.java\n[11]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/MockServletResponse.java\n[12]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/support/MockExceptionHandler.java\n[13]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/support/MockForwardOrIncludeHandler.java\n[14]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/support/MockUrlEncoder.java\n[15]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/support/ByteArrayServletInputStream.java\n[16]: https://github.com/sangupta/am/blob/master/src/main/java/com/sangupta/am/servlet/support/ByteArrayServletOutputStream.java\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsangupta%2Fam","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsangupta%2Fam","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsangupta%2Fam/lists"}