I have an object that implements ServletContextListener which, according to the Java EE servlet spec, is invoked by the servlet container at deployment/startup time via its contextInitialized(ServletContext) method.
I am trying to write a unit test that simulates a servlet container starting up and deploying my ServletContextListener (so that I can determine if the app is initializing correctly).
Is this possible, and if so, how? Is it container-specific? If so I am using OGS. Looking for code that looks something like this:
@Test
public void shouldBootstrapAppAtDeployTime() {
// Given
GlassFishContainer container = new GlassFishContainer(); // ha!
MyApp app = mock(MyApp.class); // MyApp implements ServletContextListener
// When - deploy app to container
// Causes app.contextInitialized(ServletContext) to be called
container.deploy(app);
// Then - verify the method was called with any ServletContext
mock.verify(app.contextInitialized(Matchers.any());
}
This is sloppy pseudo-code with some poorly-implemented Mockito sprinkled in for demo purposes (just to get my intentions across). Thanks in advance!
Maybe you can solve this with Arquillian: http://arquillian.org/features/
From the web page: