如何在 Arquillian 测试中模拟 UriInfo

我创建了一个 REST webservice,它使用UriInfo:


class Endpoint implements Resource {


    @Context

    private UriInfo uriInfo;



    @Override

    public Response doMagic() {

        // do magic

    }

}

部署到我的容器中,这很好用,但我也有一个 Arquillian 测试:


@Test

public void test(@ArquillianResteasyResource Resource api) throws Exception {

    try (final Response response = api.doMagic()) {

        // test magic

    }

}

抛出以下异常:


javax.ejb.EJBException: org.jboss.resteasy.spi.LoggableFailure: RESTEASY003880: Unable to find contextual data of type: javax.ws.rs.core.UriInfo

at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:186)

at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275)

at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:330)

at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:238)

at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:422)

at org.jboss.invocation.InterceptorContext$Invocation.proceed(InterceptorContext.java:509)

例外是有道理的,Arquillian 测试不测试实际的 REST web 服务,而是测试 bean。所以当然没有 webservice 上下文,其中包括UriInfo.


我想要解决这个问题,我必须以UriInfo某种方式嘲笑。但是我不知道如何模拟注入了@Context. 我怎么做?


繁花如伊
浏览 141回答 1
1回答

一只萌萌小番薯

所以它并不是完全嘲弄,但至少它现在有效。我将代码更改为以下内容:class Endpoint implements Resource {    @Override    public Response doMagic(@Context UriInfo uriInfo) {        // do magic    }}并在我的测试用例中这样调用它:@Testpublic void test(@ArquillianResteasyResource Resource api) throws Exception {    try (final Response response = api.doMagic(new TestUriInfo())) {        // test magic    }}哪里TestUriInfo返回我想要的值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java