如何测试Jersey REST Web服务?

时间:2015-03-31 01:09:15

标签: java rest unit-testing junit jersey

我编写了一个Restful Web服务,必须使用JUnit4进行测试。我已经使用Jersey Client编写了一个客户端。但是想知道我是否只能使用junit4来测试我的服务。至少有人可以帮我提供样品。

我的休息服务具有验证方法,该方法获取用户名,密码并返回令牌。

我已经为authenticate方法编写了测试用例。但我不确定如何使用网址进行测试。

public class TestAuthenticate {
    Service service  = new Service();
    String username = "user";
    String password = "password";
    String token;

    @Test(expected = Exception.class)
    public final void testAuthenticateInputs() {
        password = "pass";
        service.authenticate(username, password);
    }

    @Test(expected = Exception.class)
    public final void testAuthenticateException(){
        username = null;
        String token = service.authenticate(username, password);
        assertNotNull(token);
    }

    @Test
    public final void testAuthenticateResult() {
        String token = service.authenticate(username, password);
        assertNotNull(token);
    }
}

3 个答案:

答案 0 :(得分:20)

如果要使用URL进行测试,则需要从测试中启动服务器。您可以显式启动嵌入式服务器,这对于测试来说非常常见。像

这样的东西
public class MyResourceTest {

    public static final String BASE_URI = "http://localhost:8080/api/";
    private HttpServer server;

    @Before
    public void setUp() throws Exception {
        final ResourceConfig rc = new ResourceConfig(Service.class);
        server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);       
    }

    @After
    public void tearDown() throws Exception {
        server.stop();
    }

    @Test
    public void testService() {
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(BASE_URI).path("service");
        ...
    }
}

它基本上是一个集成测试。您正在启动Grizzly容器并将ResourceConfig加载到仅包含Service类的服务器。当然,您可以在配置中添加更多类。如果需要,可以使用“真实”资源配置。

以上测试使用此依赖

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
    <version>${jersey2.version}</version>
</dependency>

另一个选项,也就是我喜欢的选项,是使用Jersey Test Framework,它会为你启动一个嵌入式容器。测试可能看起来更像

public class SimpleTest extends JerseyTest {

    @Override
    protected Application configure() {
        return new ResourceConfig(Service.class);
    }

    @Test
    public void test() {
        String hello = target("service").request().get(String.class);
    }
}

使用此依赖

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
    <version>${jersey2.version}</version>
    <scope>test</scope>
</dependency>

嵌入式Grizzly容器将在您的ResourceConfig配置下启动。在上面的两个示例中,假设@Path类的Service值为service,您可以在测试网址中看到。

一些资源

一些例子


更新

如果您没有使用Maven,那么您需要为Jersey Test Fraemwork运行嵌入式Grizzly容器所需的罐子

enter image description here

我经常搜索所有的罐子here。您可以选择版本,下一页应该有一个链接供下载。您可以使用搜索栏搜索其他搜索栏。

这是一个简单的运行示例,一旦你拥有所有的罐子

import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.core.DefaultResourceConfig;
import com.sun.jersey.spi.container.servlet.WebComponent;
import com.sun.jersey.test.framework.JerseyTest;
import com.sun.jersey.test.framework.WebAppDescriptor;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import junit.framework.Assert;
import org.junit.Test;

public class SimpleTest extends JerseyTest {

    @Path("service")
    public static class Service {
        @GET
        public String getTest() { return "Hello World!"; }
    }

    public static class AppConfig extends DefaultResourceConfig {
        public AppConfig() {
            super(Service.class);
        }
    }

    @Override
    public WebAppDescriptor configure() {
        return new WebAppDescriptor.Builder()
                .initParam(WebComponent.RESOURCE_CONFIG_CLASS, 
                           AppConfig.class.getName())
                .build();
    }

    @Test
    public void doTest() {
        WebResource resource = resource().path("service");
        String result = resource.get(String.class);
        Assert.assertEquals("Hello World!", result);
        System.out.println(result);
    }
}

你很可能不会在测试的同一个类中拥有资源和ResourceConfig,但我只想保持简单并且在一个类中都可见。

无论您使用的是web.xml还是ResourceConfig子类(如上所示),您都可以使用测试类中内置的单独ResourceConfig来减少测试内容。完成了。否则,如果您使用的是普通的ResourceConfig课程,则只需在configure方法中替换它即可。

configure方法,只是用Java代码构建web.xml文件。您可以在WebAppDescriptor.Builder中看到不同的方法,例如initParam,这与您的网络xml中的<init-param>相同。你可以简单地在参数中使用字符串,但是有一些常量,就像我上面使用的那样。

@Test是您通常运行的JUnit测试。它正在使用Jersey客户端。但是,您只需访问Client方法,即只返回Client,而不是创建resource(),只需使用预配置的WebResource即可。如果您熟悉Jersey客户端,那么这个课程对您来说不应该是新手。

答案 1 :(得分:0)

看看Alchemy rest client generator。这可以使用场景后面的泽西客户端为您的JAX-RS Web服务类生成代理实现。实际上,您将通过单元测试将Web服务方法称为简单的Java方法。处理http身份验证。

如果您只需要简单地运行测试,就不会涉及代码生成。

demo here设置了灰熊并使用上面的生成器来运行junit测试。

免责声明:我是这个图书馆的作者。

答案 2 :(得分:0)

我认为@peeskillet已经为您提供了必要的先决条件,即您需要在嵌入式Web服务器中运行Web服务。您还可以方便地查看dropwizard或spring-boot支持。

至于实际验证响应,我会保持简单,并使用JUnit&amp; http-matcher(见https://github.com/valid4j/http-matchers