簡體   English   中英

將JMock資源與Jersey測試框架一起使用

[英]Using JMock resources with Jersey Test Framework

因為必須擴展JerseyTest,所以我無法將模擬資源添加到Jersey的ResourceConfig中。 以下代碼生成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"));
    }
}

誰能看到解決這個問題的方法?

通過將JerseyTest組合到測試類中,而不是將其子類化,可以完成此工作。 看到我的博客文章: http : //datumedge.blogspot.co.uk/2012/08/mocking-rest-resources-with-jmock-and.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM