A
A
Alexiuscrow2016-06-09 21:24:49
Java
Alexiuscrow, 2016-06-09 21:24:49

How to test websockets using the Jersey Test Framework?

There is a web service written using the Jersey framework version ' 1.18 '. I use the Jersey Test Framework for resource testing . I wrote tests for JAX-RS resources - everything works. Satisfied.
But there are also web sockets for which you also need to write tests. The problem is that when testing through the Jersey Test Framework, the websocket endpoint is not picked up and when I access it, I get 404. In a normal situation, when I build a war file and after deploying it, for example, to tomcat, everything works as it should.
The socket is annotated with the
@javax.websocket.server.ServerEndpoint annotation . The javax.websocket.server.ServerApplicationConfig interface has also been implemented, with the help of which I inform which enpoints are available:

public class EndpointsConfig implements ServerApplicationConfig {
    @Override
    public Set<Class<?>> getAnnotatedEndpointClasses(Set<Class<?>> scanned) {
        Set<Class<?>> cfgs = new HashSet<Class<?>>();
        cfgs.add(MyEndpoint.class); 
        // ...
        return cfgs;
    }
    // ...
}

The test itself:
public class WebSocketTest extends JerseyTest {

    public WebSocketTest() throws TestContainerException, IOException {
        super(new WebAppDescriptor.Builder("my.service.endpoints")
                .contextListenerClass(GuiceConfig.class)
                .addFilter(GuiceFilter.class, "guiceFilter", new TreeMap<String, String>() {{
                            put("com.sun.jersey.config.property.resourceConfigClass", "my.service.endpoints.UriExtensionsConfig");
                        }}
                )
                .build());
    }

    @Override
    protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
        return new GrizzlyWebTestContainerFactory();
    }

    @Test
    public void test() {
        // обращение к вебсокету
    }
}

How to tell the Jersey Test Framework that there is a specific websocket endpoint? I suspect that I need to specify something in the WebAppDescriptor instance (I pass it as an argument to the superclass constructor of the WebSocketTest class), but what?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question