让我们假设你的Application
样子:
@ApplicationPath("/")
public class MyApplication extends ResourceConfig {
/**
* Register JAX-RS application components.
*/
public MyApplication () {
// Register RequestContextFilter from Spring integration module.
register(RequestContextFilter.class);
// Register JAX-RS root resource.
register(JerseySpringResource.class);
}
}
您的 JAX-RS 根资源,例如:
@Path("spring-hello")
public class JerseySpringResource {
@Autowired
private GreetingService greetingService;
@Inject
private DateTimeService timeService;
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getHello() {
return String.format("%s: %s", timeService.getDateTime(), greetingService.greet("World"));
}
}
而且你有helloContext.xml
直接从你的类路径中命名的弹簧描述符。现在您想getHello
使用 Jersey 测试框架测试您的资源方法。您可以像这样编写测试:
public class JerseySpringResourceTest extends JerseyTest {
@Override
protected Application configure() {
// Enable logging.
enable(TestProperties.LOG_TRAFFIC);
enable(TestProperties.DUMP_ENTITY);
// Create an instance of MyApplication ...
return new MyApplication()
// ... and pass "contextConfigLocation" property to Spring integration.
.property("contextConfigLocation", "classpath:helloContext.xml");
}
@Test
public void testJerseyResource() {
// Make a better test method than simply outputting the result.
System.out.println(target("spring-hello").request().get(String.class));
}
}