Open vrish88 opened 2 years ago
SessionRepositoryFilter
doesn't integrate with MockMvc#session
.
The reason is because SessionRepositoryFilter
uses a CookieHttpSessionIdResolver
, which looks for a cookie named "SESSION" and MockMvc#session
does not provide that cookie.
At minimum we can update our documentation to explain that.
In your scenario it doesn't seem necessary for the request to pass through the SessionRepositoryFilter
.
As you mentioned, one way to get around that is by providing a MapSessionRepository
bean.
I wouldn't recommend this approach, because it's not obvious why this fixes the problem.
For background, this approach works because it deactivates Spring Boot's JdbcSessionConfiguration
, which is what creates the SessionRepositoryFilter
.
Adding the MapSessionRepository
bean means the SessionRepositoryFilter
won't be created and you won't run into the mapping problem between HttpSession
and Session
.
A better approach is to explicitly exclude the Spring Boot auto-configuration in your tests.
One way to do that is by setting the spring.autoconfigure.exclude
property on your test class.
@SpringBootTest(properties = "spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.session.SessionAutoConfiguration")
Another option is to exclude your DataSource
bean from the tests.
In the given example you are using an embedded H2 database, but I presume you will switch to a different database when the code is production ready.
With an H2 database Spring Boot will auto-configure the DataSource
for you.
However, with other types of databases you will register the DataSource
bean yourself.
If there is no DataSource
bean present when you run your tests, then the auto-configuration will not create a SessionRepositoryFilter
.
You can confirm this in the provided sample by noticing that the tests pass after removing the 'org.springframework.boot:spring-boot-starter-data-jdbc'
and 'com.h2database:h2'
dependencies.
@eleftherias thanks for the response. It's good to know we diagnosed actual behavior.
Is there something that can be done in addition to updating the documentation? Working with devs new to spring, we've lost a lot of time to debugging why this doesn't work. Could there be a warning/error when MockMvc#session
and SessionRepositoryFilter
are used together?
I would love to help/contribute here.
Describe the bug When using spring-session-jdbc, MockMvc's
MockHttpServletRequestBuilder#session
is ignored. It seems as though onceSessionRepositoryFilter
is added to the filter chain, it ignores any session configured bymockMvc
To Reproduce Download the github repo demoing the problem and run
./gradlew test
. To fix the test, add theMapSessionRepository
bean to the context.Expected behavior From the demo repo, I would have expected the session that was provided through
mockMvc
would be the session that was received by the controller.Sample Github repo and, for posterity, inlining the relevant files.
build.gradle
DemoApplication.java
DemoApplicationTests.java