Why I cannot create endpoint with RestEasy

2019-09-11 07:27发布

问题:

I'm trying to implement a test for my REST endpoint, described here: http://antoniogoncalves.org/2012/12/19/test-your-jax-rs-2-0-web-service-uris-without-mocks/. Mentioned solution uses Jersey implementation of JAX-RS, but I want to use RestEasy. When I run my test I get

java.lang.UnsupportedOperationException
at org.jboss.resteasy.spi.ResteasyProviderFactory.createEndpoint(ResteasyProviderFactory.java:2176)

Any idea why JBoss's implementation of JAX-RS does not support creating endpoints, but Jersey's does (as it is under the link from the beginning of my post)?

回答1:

Take a look at the RESTeasy documentation, Chapter 36. Embedded Containers. You will find examples for four different types of containers and their usage in testing:

  • Undertow
  • Sun HttpServer
  • TJWS
  • Netty

You can pick your flavor.

Here's a complete example using the Sun HttpServer (as in the example you linked to):

public class SunHttpServerTest {

    @Path("simple")
    public static class SimpleResource {

        @GET
        public String get() {
            return "Hello Sun";
        }
    }

    private HttpContextBuilder contextBuilder;
    private HttpServer httpServer;

    @Before
    public void setUp() throws Exception {
        httpServer = HttpServer.create(new InetSocketAddress(8000), 10);
        contextBuilder = new HttpContextBuilder();
        contextBuilder.getDeployment().getActualResourceClasses().add(SimpleResource.class);
        HttpContext context = contextBuilder.bind(httpServer);
        context.getAttributes().put("some.config.info", "42");
        httpServer.start();
    }

    @After
    public void tearDown() {
        contextBuilder.cleanup();
        httpServer.stop(0);
    }

    @Test
    public void shouldReturnCorrectMessage() {
        Client client = ClientBuilder.newClient();
        Response response = client.target("http://localhost:8000/simple")
                .request().get();
        assertEquals(200, response.getStatus());
        String message = response.readEntity(String.class);
        assertEquals("Hello Sun", message);
        System.out.println(message);
        response.close();
    }
}

Also needed for this to work is the following dependency

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-jdk-http</artifactId>
    <version>3.0.9.Final</version>
</dependency>