Ответ 1
Предположим, что ваш 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"));
}
}
И у вас есть дескриптор spring с именем 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));
}
}