Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>ServletRequest</code>, <code>ServletResponse</code> and <code>FilterChain</code> are all interfaces, so you can easily create test stubs for them, either by hand or using a mocking framework.</p> <p>Make the mock objects configurable so that you can prepare a canned response to <code>getRequestURI()</code> and so that you can query the ServletResponse to assert that sendRedirect has been invoked. </p> <p>Inject a mock ModeService.</p> <p>Invoke doFilter passing the mock ServletRequest, ServletResponse and FilterChain as its parameters.</p> <pre><code>@Test public void testSomethingAboutDoFilter() { MyFilter filterUnderTest = new MyFilter(); filterUnderTest.setModeService(new MockModeService(ModeService.ONLINE)); MockFilterChain mockChain = new MockFilterChain(); MockServletRequest req = new MockServletRequest("/maintenance.jsp"); MockServletResponse rsp = new MockServletResponse(); filterUnderTest.doFilter(req, rsp, mockChain); assertEquals("/maintenance.jsp",rsp.getLastRedirect()); } </code></pre> <p>In practice you'll want to move the setup into an @Before <code>setUp()</code> method, and write more @Test methods to cover every possible execution path. </p> <p>... and you'd probably use a mocking framework like JMock or Mockito to create mocks, rather than the hypothetical <code>MockModeService</code> etc. I've used here.</p> <p>This is a <em>unit testing</em> approach, as opposed to an integration test. You are only exercising the unit under test (and the test code).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload