为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 Arquillian 测试中模拟 UriInfo

如何在 Arquillian 测试中模拟 UriInfo

繁花如伊 2021-09-29 16:21:16
我创建了一个 REST webservice,它使用UriInfo:class Endpoint implements Resource {    @Context    private UriInfo uriInfo;    @Override    public Response doMagic() {        // do magic    }}部署到我的容器中,这很好用,但我也有一个 Arquillian 测试:@Testpublic 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.UriInfoat 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. 我怎么做?
查看完整描述

1 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

所以它并不是完全嘲弄,但至少它现在有效。我将代码更改为以下内容:


class Endpoint implements Resource {


    @Override

    public Response doMagic(@Context UriInfo uriInfo) {

        // do magic

    }

}

并在我的测试用例中这样调用它:


@Test

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

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

        // test magic

    }

}

哪里TestUriInfo返回我想要的值。


查看完整回答
反对 回复 2021-09-29
  • 1 回答
  • 0 关注
  • 140 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信