<p>I have a bunch of servlets running under the Tomcat servlet container. I would like to separate test code from production code, so I considered using a test framework.
JUnit is nicely integrated into Eclipse, but I failed to make it run servlets using a running Tomcat server. Could you please recommend a unit testing framework that supports testing Tomcat servlets? Eclipse integration is nice but not necessary. </p>

[{'answer_id': 53535, 'author': 'Will Sargent', 'author_id': 5266, 'author_profile': 'https://Stackoverflow.com/users/5266', 'pm_score': 3, 'selected': False, 'text': '<p>Okay.  Ignoring the \'tomcat\' bit and coding to the servlet, your best bet is to create mocks for the response and request objects, and then tell it what you expect out of it.</p>\n\n<p>So for a standard empty doPost, and using <a href="http://www.easymock.org" rel="noreferrer">EasyMock</a>, you\'ll have</p>\n\n<pre><code>public void testPost() {\n   mockRequest = createMock(HttpServletRequest.class);\n   mockResponse = createMock(HttpServletResponse.class);\n   replay(mockRequest, mockResponse);\n   myServlet.doPost(mockRequest, mockResponse);\n   verify(mockRequest, mockResponse);\n}\n</code></pre>\n\n<p>Then start adding code to the doPost.  The mocks will fail because they have no expectations, and then you can set up the expectations from there.</p>\n\n<p>Note that if you want to use EasyMock with classes, you\'ll have to use the EasyMock class extension library.  But it\'ll work the same way from then on.</p>\n'}, {'answer_id': 53550, 'author': 'Argelbargel', 'author_id': 2992, 'author_profile': 'https://Stackoverflow.com/users/2992', 'pm_score': 1, 'selected': False, 'text': '<p>For "in-container" testing, have a look at <a href="http://jakarta.apache.org/cactus/" rel="nofollow noreferrer">Cactus</a></p>\n\n<p>If you want to be able to test without a running container you can either simulate its components with your own mockobjects (e.g. with <a href="http://www.easymock.org" rel="nofollow noreferrer">EasyMock</a>) or you could try <a href="http://mockrunner.sourceforge.net/examplesservlets.html" rel="nofollow noreferrer">MockRunner</a> which has "pre-defined" Stubs for testing servlets, jdbc-connections etc.</p>\n'}, {'answer_id': 85947, 'author': 'Scott Bale', 'author_id': 2495576, 'author_profile': 'https://Stackoverflow.com/users/2495576', 'pm_score': 4, 'selected': True, 'text': '<p>Check out <a href="http://httpunit.sourceforge.net/doc/servletunit-intro.html" rel="noreferrer">ServletUnit</a>, which is part of HttpUnit.  In a nutshell, ServletUnit provides a library of mocks and utilities you can use in ordinary JUnit tests to mock out a servlet container and other servlet-related objects like request and response objects.  The link above contains examples.</p>\n'}, {'answer_id': 158237, 'author': 'WMR', 'author_id': 2844, 'author_profile': 'https://Stackoverflow.com/users/2844', 'pm_score': 3, 'selected': False, 'text': '<p>The Spring Framework has nice ready made mock objects for several classes out of the Servlet API:</p>\n\n<p><a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/mock/web/package-summary.html" rel="noreferrer">http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/mock/web/package-summary.html</a></p>\n'}, {'answer_id': 17023707, 'author': 'Raedwald', 'author_id': 545127, 'author_profile': 'https://Stackoverflow.com/users/545127', 'pm_score': 2, 'selected': False, 'text': '<p>Separate the parts of that code that deal with HTTP requests and response from the parts that do business logic or data-base manipulation. In most cases this will produce a three tier architecture, with a data-layer (for the data-base/persistence), service-layer (for the business logic) and a presentation-layer (for the HTTP requests and responses).</p>\n\n<ol>\n<li>You can unit test the first two layers without any servlet stuff at all; it will be easier to test that way.</li>\n<li>You can test the presentation layer, <a href="https://stackoverflow.com/a/158237/545127">as others suggest</a>, using mock HTTP request and response objects.</li>\n<li>Finally, if you feel it really is necessary, you can do integration tests using a too such as <a href="http://htmlunit.sourceforge.net/" rel="nofollow noreferrer">HtmlUnit</a> or <a href="http://jwebunit.sourceforge.net/" rel="nofollow noreferrer">JWebUnit</a> .</li>\n</ol>\n'}, {'answer_id': 17351213, 'author': 'Mike Kaufman', 'author_id': 2529265, 'author_profile': 'https://Stackoverflow.com/users/2529265', 'pm_score': 0, 'selected': False, 'text': '<p>Updated Feb 2018: <a href="https://closingbraces.net/openbrace/" rel="nofollow noreferrer">OpenBrace Limited has closed down</a>, and its ObMimic product is no longer supported.</p>\n\n<p>If you want a newer alternative to ServletUnit for JUnit testing of Servlets, you might find my company\'s <a href="http://www.openbrace.com" rel="nofollow noreferrer">ObMimic</a> library useful. It\'s available for free from the website\'s <a href="http://www.openbrace.com/main/pages/download/download-current.html" rel="nofollow noreferrer">downloads</a> page.</p>\n\n<p>As with ServletUnit, it provides a library of classes that you can use in normal JUnit or TestNG tests outside of any servlet container to simulate the Servlet API.</p>\n\n<p>Its Servlet API objects have no-argument constructors, are fully configurable and inspectable for all relevant Servlet API data and settings, and provide a complete simulation of all of the behaviour specified by the Servlet API\'s javadoc. To help with testing there\'s support for selective recording of Servlet API calls, control over any container-dependent behaviour, checks for any ambiguous calls (i.e. where the Servlet API behavour isn\'t fully defined), and an in-memory JNDI simulation for any servlet code that relies on JNDI lookups.</p>\n\n<p>For full details, example code, "how to" guides, Javadoc etc, see the website.</p>\n'}]
['https://Stackoverflow.com/questions/53532', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1702/']