[英]Using JMock resources with Jersey Test Framework
Because JerseyTest has to be extended, I've been unable to get a mock resource into Jersey's ResourceConfig. 因为必须扩展JerseyTest,所以我无法将模拟资源添加到Jersey的ResourceConfig中。 The following code generates a NullPointerException because mockResource has yet to be initialised:
以下代码生成NullPointerException,因为尚未初始化嘲笑资源:
@Path("/")
public interface MyResource {
@GET String get();
}
public class MockResourceTest extends JerseyTest {
@Rule public JUnitRuleMockery context = new JUnitRuleMockery();
private MyResource mockResource = context.mock(MyResource.class);
@Override
protected TestContainerFactory getTestContainerFactory() throws TestContainerException {
return new GrizzlyTestContainerFactory();
}
@Override
protected AppDescriptor configure() {
DefaultResourceConfig resourceConfig = new DefaultResourceConfig();
// FIXME: configure() is called from superclass constructor, so mockResource is still null!
resourceConfig.getSingletons().add(mockResource);
return new LowLevelAppDescriptor.Builder(resourceConfig).build();
}
@Test
public void respondsToGetRequest() {
context.checking(new Expectations() {{
allowing(mockResource).get(); will(returnValue("foo"));
}});
String actualResponse = client().resource("http://localhost:9998/").get(String.class);
assertThat(actualResponse, is("foo"));
}
}
Can anyone see a way round this? 谁能看到解决这个问题的方法?
I've got this working by composing JerseyTest into the test class, rather than subclassing it. 通过将JerseyTest组合到测试类中,而不是将其子类化,可以完成此工作。 See my blog post: http://datumedge.blogspot.co.uk/2012/08/mocking-rest-resources-with-jmock-and.html
看到我的博客文章: http : //datumedge.blogspot.co.uk/2012/08/mocking-rest-resources-with-jmock-and.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.