Testing a JAX-RS Web Service?

前端 未结 10 1204
[愿得一人]
[愿得一人] 2020-12-04 08:36

I\'m currently looking for ways to create automated tests for a JAX-RS (Java API for RESTful Web Services) based web service.

I basically need a way to send it cert

相关标签:
10条回答
  • 2020-12-04 09:38

    You probably wrote some java code that implements your business logic and then you have generated the web services end point for it.

    An important thing to do is to independently test your business logic. Since it's pure java code you can do that with regular JUnit tests.

    Now, since the web services part is just an end point, what you want to make sure is that the generated plumbing (stubs, etc) are in sync with your java code. you can do that by writing JUnit tests that invoke the generated web service java clients. This will let you know when you change your java signatures without updating the web services stuff.

    If your web services plumbing is automatically generated by your build system at every build, then it may not be necessary to test the end points (assuming it's all properly generated). Depends on your level of paranoia.

    0 讨论(0)
  • 2020-12-04 09:38

    Take a look at Alchemy rest client generator. This can generate a proxy implementation for your JAX-RS webservice class using jersey client behind the scene. Effectively you will call you webservice methods as simple java methods from your unit tests. Handles http authentication as well.

    There is no code generation involved if you need to simply run tests so it is convenient.

    Dislclaimer: I am the author of this library.

    0 讨论(0)
  • 2020-12-04 09:40

    I use Apache's HTTPClient (http://hc.apache.org/) to call Restful Services. The HTTP Client library allows you to easily perform get, post or whatever other operation you need. If your service uses JAXB for xml binding, you can create a JAXBContext to serialize and deserialize inputs and outputs from the HTTP request.

    0 讨论(0)
  • 2020-12-04 09:41

    As James said; There is built-in test framework for Jersey. A simple hello world example can be like this:

    pom.xml for maven integration. When you run mvn test. Frameworks start a grizzly container. You can use jetty or tomcat via changing dependencies.

    ...
    <dependencies>
      <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.16</version>
      </dependency>
    
      <dependency>
        <groupId>org.glassfish.jersey.test-framework</groupId>
        <artifactId>jersey-test-framework-core</artifactId>
        <version>2.16</version>
        <scope>test</scope>
      </dependency>
    
      <dependency>
        <groupId>org.glassfish.jersey.test-framework.providers</groupId>
        <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
        <version>2.16</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
    ...
    

    ExampleApp.java

    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    
    @ApplicationPath("/")
    public class ExampleApp extends Application {
    
    }
    

    HelloWorld.java

    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
    
    @Path("/")
    public final class HelloWorld {
    
        @GET
        @Path("/hello")
        @Produces(MediaType.TEXT_PLAIN)
        public String sayHelloWorld() {
    
            return "Hello World!";
        }
    }
    

    HelloWorldTest.java

    import org.glassfish.jersey.server.ResourceConfig;
    import org.glassfish.jersey.test.JerseyTest;
    import org.junit.Test;
    import javax.ws.rs.core.Application;
    import static org.junit.Assert.assertEquals;
    
    public class HelloWorldTest extends JerseyTest {
    
        @Test
        public void testSayHello() {
    
            final String hello = target("hello").request().get(String.class);
    
            assertEquals("Hello World!", hello);
        }
    
        @Override
        protected Application configure() {
    
            return new ResourceConfig(HelloWorld.class);
        }
    }
    

    You can check this sample application.

    0 讨论(0)
提交回复
热议问题