So I have a session scoped bean and naturally, I'm getting the error you would expect in my unit tests.
java.lang.IllegalStateException: No Scope registered for scope 'Session'
What is the proper Java based way to mock a session for a unit test?
This was the old way:
Spring Test session scope bean using Junit
The thing is though that I don't need to test a controller. I'm testing a component with a session scoped bean in it. I just need a fake session so that it works.
It was this easy :-/
In the test:
public void setup() throws Exception {
ConfigurableBeanFactory factory = (ConfigurableBeanFactory) wac.getAutowireCapableBeanFactory();
factory.registerScope("session", new SessionScope());
}
Then you call that before every test that needs it. Could just use @Before if you are using JUnit.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.