如何测试Jersey REST Web服务?

Jen*_*nny 17 java rest junit unit-testing jersey

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

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

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Pau*_*tha 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");
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

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

上述测试使用此依赖项

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-grizzly2-http</artifactId>
    <version>${jersey2.version}</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

另一种选择,我更喜欢的是使用Jersey测试框架,它将为您启动一个嵌入式容器.测试可能看起来更像

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此依赖项

<dependency>
    <groupId>org.glassfish.jersey.test-framework.providers</groupId>
    <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
    <version>${jersey2.version}</version>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

嵌入式Grizzly容器将在您的ResourceConfig配置下启动.在上面的两个示例中,假设类的@PathServiceservice,您可以在测试URL中看到.

一些资源

一些例子


UPDATE

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

在此输入图像描述

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

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

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

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