如何测试Jersey REST Web服务?

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

我的rest服务具有validation方法,该方法获取用户名,密码并返回令牌。

我已经为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); } } 

如果要使用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类的服务器。 当然,您可以在配置中添加更多类。 如果需要,您可以使用“真实”资源配置。

以上测试使用此依赖项

  org.glassfish.jersey.containers jersey-container-grizzly2-http ${jersey2.version}  

另一个选择,我更喜欢的是使用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); } } 

使用此依赖项

  org.glassfish.jersey.test-framework.providers jersey-test-framework-provider-grizzly2 ${jersey2.version} test  

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

一些资源

  • Jersey 2 Test Framework用户指南

一些例子

  • 如何使用Jersey 2测试框架为此类编写unit testing
  • 如何在内存unit testingSpring-Jersey
  • Mockito,Test Framework和Jersey 2的示例
  • Mockito,Test Framework和Jersey 1的示例

UPDATE

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

在此处输入图像描述

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

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

 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 ,它与Web xml中的相同。 你可以简单地在参数中使用字符串,但是有一些常量,正如我上面使用的那样。

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

看看Alchemy rest客户端生成器 。 这可以使用场景后面的泽西客户端为您的JAX-RS Web服务类生成代理实现。 实际上,您将从unit testing中将Web服务方法称为简单的Java方法。 处理http身份validation。

如果您需要简单地运行测试以便于方便,则不会涉及代码生成。

这里的演示设置了灰熊并使用上面的生成器来运行junit测试。

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

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

至于实际validation响应,我会保持简单,并使用JUnit和http-matcher(参见https://github.com/valid4j/http-matchers )