Working with Web Mocks
To provide comprehensive web testing support, the TestContext framework has a
MockTestExecutionListener
that is enabled by default. When testing against a
WebApplicationContext
, this TestExecutionListener
sets up default thread-local state by using Infra Web’s RequestContextHolder
before
each test method and creates a HttpMockRequestImpl
, a MockHttpResponseImpl
, and
a ServletWebRequest
based on the base resource path configured with
@WebAppConfiguration
. MockTestExecutionListener
also ensures that the
MockHttpResponseImpl
and ServletWebRequest
can be injected into the test instance,
and, once the test is complete, it cleans up thread-local state.
Once you have a WebApplicationContext
loaded for your test, you might find that you
need to interact with the web mocks — for example, to set up your test fixture or to
perform assertions after invoking your web component. The following example shows which
mocks can be autowired into your test instance. Note that the WebApplicationContext
and
MockContextImpl
are both cached across the test suite, whereas the other mocks are
managed per test method by the MockTestExecutionListener
.
-
Injecting mocks
@JUnitWebConfig
class WacTests {
@Autowired
WebApplicationContext wac; // cached
@Autowired
MockServletContext servletContext; // cached
@Autowired
MockHttpSession session;
@Autowired
MockHttpServletRequest request;
@Autowired
MockHttpServletResponse response;
@Autowired
ServletWebRequest webRequest;
//...
}