Spring has supported request- and session-level scoped beans since early versions, so you can test such beans by following these steps:
-
Make sure
WebApplicationContext
is loaded for your test by annotating your test class with@WebAppConfiguration
. -
Inject a mock request or session into your test instance and prepare your test bench accordingly.
-
Call your web component that you got from the configured
WebApplicationContext
(using dependency injection). -
Perform assertions on mock objects.
The following code snippet shows the XML configuration for the login use case. Note that the userService
bean has a dependency on the loginAction
bean, which is scoped at the request level. Additionally, a
LoginAction
instance is created using SpEL expressions that receive
username and password from the current HTTP request. In our test, we need to configure these request parameters
through a mock object managed by the TestContext framework. The following listing shows the configuration for this
use case:
<beans>
<bean id="userService" class="com.example.SimpleUserService"
c:loginAction-ref="loginAction"/>
<bean id="loginAction" class="com.example.LoginAction"
c:username="#{request.getParameter('user')}"
c:password="#{request.getParameter('pswd')}"
scope="request">
<aop:scoped-proxy/>
</bean>
</beans>
In RequestScopedBeanTests
we inject both UserService
(that is, the object under test) and
MockHttpServletRequest
into our test instance. In the requestScope()
test method, we set
up our testbench by setting the request parameters in the provided MockHttpServletRequest
. If the
loginUser()
method is called on our userService
, we will ensure that the special service
has access to the request-level loginAction
bean or the current MockHttpServletRequest
(that
is, the one in which we just set the parameters). You can then make assertions about the results based on the known
inputs for the username and password. The following listing shows how to do this:
@SpringJUnitWebConfig
class RequestScopedBeanTests {
@Autowired UserService userService;
@Autowired MockHttpServletRequest request;
@Test
void requestScope() {
request.setParameter("user", "enigma");
request.setParameter("pswd", "$pr!ng");
LoginResults results = userService.loginUser();
// confirm the results
}
}
@SpringJUnitWebConfig
class RequestScopedBeanTests {
@Autowired lateinit var userService: UserService
@Autowired lateinit var request: MockHttpServletRequest
@Test
fun requestScope() {
request.setParameter("user", "enigma")
request.setParameter("pswd", "\$pr!ng")
val results = userService.loginUser()
// confirm the results
}
}
The following code snippet is similar to what we saw earlier for a bean that is scoped at the request level. However,
this time the userService
bean has a dependency on the userPreferences
bean, which is
scoped at the session level. Note that the UserPreferences
bean instance is created using a SpEL
expression that retrieves the topic from the current HTTP session. In our test, we need to set up a theme in a mock
session managed by the TestContext framework. The following example shows how to do this:
<beans>
<bean id="userService" class="com.example.SimpleUserService"
c:userPreferences-ref="userPreferences" />
<bean id="userPreferences" class="com.example.UserPreferences"
c:theme="#{session.getAttribute('theme')}"
scope="session">
<aop:scoped-proxy/>
</bean>
</beans>
In SessionScopedBeanTests
we inject UserService
and MockHttpSession
into our
test instance. In the sessionScope()
test method, we set up our testbench by setting the expected
theme
attribute in the provided MockHttpSession
. If the
processUserPreferences()
method is called on our userService
, we will ensure that the
custom service has access to the session-scoped userPreferences
bean for the current MockHttpSession
and we can make assertions about the results based on the configured theme. The following example shows how to do
this:
@SpringJUnitWebConfig
class SessionScopedBeanTests {
@Autowired UserService userService;
@Autowired MockHttpSession session;
@Test
void sessionScope() throws Exception {
session.setAttribute("theme", "blue");
Results results = userService.processUserPreferences();
// confirm the results
}
}
@SpringJUnitWebConfig
class SessionScopedBeanTests {
@Autowired lateinit var userService: UserService
@Autowired lateinit var session: MockHttpSession
@Test
fun sessionScope() {
session.setAttribute("theme", "blue")
val results = userService.processUserPreferences()
// confirm the results
}
}
GO TO FULL VERSION