【问题标题】:JAX-RS Response.getEntity() always nullJAX-RS Response.getEntity() 始终为空
【发布时间】:2020-09-15 19:59:16
【问题描述】:

我需要 Arquillian 测试方面的帮助。

添加情况的代码示例。 此代码在真实环境中运行正常。只有在使用 arquillian 制作的测试用例中,才会出现预期结果

代码:

@Stateless
public class CustomerResourceImpl implements CustomerResource{
    @Override
    public Response findOne(String id) {
        String res = "Un cliente";
        return Response.ok(res).build();
    }
}


@Path("customer")
@Produces(MediaType.APPLICATION_JSON)
public interface CustomerResource {
    
    @GET
    @Path("/findOne")
    public javax.ws.rs.core.Response findOne(@QueryParam("id") String id);

}

还有这个测试用例

@RunWith(Arquillian.class)
public class CustomerResourceTest {

    @Deployment (testable = false)
    public static Archive createTestArchive() {
        return ShrinkWrap
                ..... (mas)
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
    
    @ArquillianResource
    private URL deploymentURL;

    @Test
    @RunAsClient
    public void findOne(@ArquillianResteasyResource CustomerResource resource) throws Exception {
        final Response response = resource.findOne("1");
        System.out.println(response.getEntity()); // IS NULL ??
        System.out.println(response.getStatus()); // 200 OK
        assertNotNull(response);
    }       
}

问题是 response.getEntity() 总是 NULL 。为什么?状态响应 OK = 200 ,没问题。此服务在带有 Java 8 的 jboss 7.2 中运行正常。

谢谢!

【问题讨论】:

标签: jax-rs


【解决方案1】:

原因是@Deployment (testable = false)

@Deployment 说:

testable = 定义是否应根据协议打包此部署,以便测试用例可以在容器中执行。

所以 false 表示它不会部署在容器中,因此当您的测试在容器内运行时将为 null。

我建议使用@Deployment 不传入参数而不是设置testable = true

经过几天的摆弄,我今天刚刚解决了这个问题。我想我从 Internet 上的示例中粘贴了 @Deployment (testable = false) 代码,我不明白这些代码希望能发挥作用。

【讨论】:

  • Bae,谢谢您的回复,但无法正常工作。我在这个链接中找到了解决方案:stackoverflow.com/questions/48781860/…
  • 啊,那你能自己写答案吗?我会用正确的问题更新标题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多