-1

I use com.sun.net.httpserver.HttpServer in integration tests. It does its job but I've noticed that ServletContextListener's methods aren't invoked during tests. When I deploy the app to the real Tomcat server I can see its methods being called.

Below is the listener class:

package abc;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class StartupListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent event) {
        System.out.println("########################################");
    }

    @Override
    public void contextDestroyed(ServletContextEvent event) {
        System.out.println("########################################");
    }
}

Here is how I start the HttpServer in the test:

@BeforeClass
public static void startServer() {
    URI uri = UriBuilder.fromUri("http://localhost/").port(8080).build();

    // Create an HTTP server listening at port 8080
    try {
        server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0);
    } catch (IOException e) {
        e.printStackTrace();
        fail();
    }

    // Create a handler wrapping the JAX-RS application
    HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new ApplicationConfig(), HttpHandler.class);
    // Map JAX-RS handler to the server root
    server.createContext(uri.getPath(), handler);
    // Start the server
    server.start();
}

Maven dependency:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

I removed @WebListener and inserted

metadata-complete="false"

and

<listener>
        <listener-class>abc.StartupListener</listener-class>
</listener>

in web.xml but it didn't help

What is the problem? Is there anything that needs to be setup?

ka3ak
  • 2,435
  • 2
  • 30
  • 57
  • I'm not sure if and how it is possible to create a servlet container with the `HttpServer` used here. Isn't that a use case for Jetty? – Selaron Dec 19 '18 at 09:18
  • You seem to think that `com.sun.net.httpserver.HttpServer` is a Servlet-compliant servlet container. It isn't. – user207421 Dec 19 '18 at 09:19

1 Answers1

5

What is the problem?

HttpServer is not a servlet container and does not know about servlet api.

Is there anything that needs to be setup?

You need to setup a servlet container like jetty, which is frequently used for unit testing java based web applications.

Selaron
  • 6,105
  • 4
  • 31
  • 39
  • Thank you. However I'll try it later as I've already created a unit-test that tests the listener class directly. – ka3ak Dec 20 '18 at 10:21
  • You are welcome. If this question is answered, please mark it answered. If you encounter issues on setting up sevlet container environment for JUnit testing, feel free to research or ask new questions on that topic. – Selaron Jan 15 '19 at 10:02