这是一个示例Resource类:
@Path("/resource")
public class SomeResource {
@GET
@Produces({MediaType.APPLICATION_XML})
public String someMethod(@QueryParam("param1") String param1, ..., @Context HttpServletRequest request) {
String remoteUser = request.getRemoteAddr();
// Business logic here.
return response;
}
}
Run Code Online (Sandbox Code Playgroud)
而JerseyTest的资源:
public class TestSomeResource extends JerseyTest {
@Override
protected Application configure() {
enable(TestProperties.LOG_TRAFFIC);
return new ResourceConfig(SomeResource.class);
}
@Test
public void testXMLResponse() {
String response = target("resource")
.queryParam("param1", param1)
// More parameters here.
.request()
.accept(MediaType.APPLICATION_XML)
.get(String.class);
// Some assertions on response.
}
}
Run Code Online (Sandbox Code Playgroud)
除了@Context HttpServletRequest用作输入参数的资源外,我能够为所有其他资源运行泽西测试.它给了一个InternalServerErrorException: HTTP 500 Internal …
我正在使用Spring-Jersey3,并且无法弄清楚如何使用Spring bean对RESTFul API进行单元测试
调节器
package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.service.DataSource;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("test")
@Component
public class SpringController {
@Autowired
private DataSource datasource;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return new String(datasource.load());
}
}
Run Code Online (Sandbox Code Playgroud)
服务接口
package com.service;
public interface DataSource {
public String load();
}
Run Code Online (Sandbox Code Playgroud)
服务实施
package com.service;
import org.springframework.stereotype.Repository;
@Repository
public class DataSourceImpl implements DataSource {
@Override
public String load() {
return "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
ResourceRegister.java(泽西资源注册)
package com.component;
import org.glassfish.jersey.server.ResourceConfig;
import com.controller.SpringController; …Run Code Online (Sandbox Code Playgroud) jersey ×2
java ×1
java-ee ×1
jax-rs ×1
jersey-2.0 ×1
junit ×1
rest ×1
spring ×1
unit-testing ×1