The Servlet container decides when to dispatch events to the HttpSessionListener
. This is not necessarily right after a session created or destroyed. Because this depends on the container implementation, you can't depend on the unit test. Integration testing is the way to go.
You can always unit test those HttpSessionListener
implementations
HttpSessionListener listener = new MyHttpSessionListener();
listener.sessionCreated(mockEvent);
listener.sessionDestroyed(mockEvent);
outside the context of your application.