如果您想使用 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 容器并仅使用 Service 类将 ResourceConfig 加载到服务器。当然,您可以在配置中添加更多类。如果需要,您可以使用“真实”资源配置。
上面的测试使用了这个依赖
<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 配置。在上面的两个示例中,假定 Service 类的 @Path 值为 service,正如您在测试 URL 中看到的那样。
一些资源
一些例子
更新
如果您不使用 Maven,以下是您需要为 Jersey Test Fraemwork 运行嵌入式 Grizzly 容器的 jars
我通常搜索我所有的罐子here。您可以选择版本,下一页应该有一个链接,可以下载。您可以使用搜索栏搜索其他人。
这是一个简单的运行示例,一旦你拥有了所有的 jars
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 中的<init-param> 相同。您可以简单地在参数中使用字符串,但也有一些常量,就像我在上面使用的那样。
@Test 是您通常会运行的 JUnit 测试。它正在使用泽西客户端。但您无需创建Client,只需访问resource() 方法即可使用预配置的Client,该方法返回WebResource。如果您熟悉 Jersey Client,那么这门课对您来说应该不陌生。