用于测试休息资源的Junit测试用例

时间:2016-03-02 14:37:26

标签: java rest junit

public class SignLitsource {
        @GET
        @Path("token")
        @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
        @PreAuthorize("isAuthenticated()")
        public Response eSignLiterature(
                @SafeString@HeaderParam("CLIENT_ROID")String roidString) {
            SignatureEmailRetriver emailRetriever = new SignatureEmailRetriver();

            String emailAddress = emailRetriever.retrieveEmailAddress(Long.valueOf(poidString));
            String pin = SignaturePin.generatePin();
            SignLiterature signLiterature = new SignLiterature();
            signLiterature.setEmailAddress(emailAddress);
            signLiterature.setPin(pin);
            return Response.ok(signLiterature).build();
        }

    }

试着弄清楚如何编写一个Junit测试用例。想要为rest资源编写测试用例时需要。

1 个答案:

答案 0 :(得分:0)

您可以使用Jersey WebTarget Client api
示例代码:

WebTarget target = target("token"); // endpoint url
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
javax.ws.rs.core.Response response = builder.get(); // builder.post();
SignLiterature returnType = response.readEntity(new GenericType<SignLiterature>);

asserNotNull(returnType);