简体   繁体   English

测试Rest Api方法

[英]Testing Rest Api methods

I'have to test the functions in rest api class that i created, I'm unable to get the functions of the created api class inside the test class, I haven't do the testing coding before. 我必须测试我创建的rest api类中的功能,无法在测试类中获取所创建的api类的功能,我之前没有进行测试编码。 I need some guides to follow . 我需要一些指导。

@POST @POST

@Produces("text/plain")
@Path("/notifications/login/")
@HeaderParam("encoded")
Response login(@HeaderParam("encoded") String encoded, @QueryParam("tenantId") String tenantId) throws NotificationManagementException;

Its a function that i created, im not giving whole function body here, i need to know how to test this function. 它是我创建的功能,在这里我没有给出整个功能体,我需要知道如何测试该功能。 I'm giving a request call in javascript. 我正在用javascript发出请求调用。

Unless there is some reason why you really don't want to, you could make the method public , and then you would be able to see it inside the test class. 除非出于某些原因您确实不想这么做,否则可以将方法设为public ,然后可以在测试类中看到它。

ie

public Response login(...)

In the test you can then make a new instance of the class and call the method normally. 然后,在测试中,您可以创建该类的新实例并正常调用该方法。

eg 例如

@Test
public void shouldDoSomething(){
  SomeClass someClass = new SomeClass();
  someClass.login(...);
  //some assertions or verifications
}

Two ways: 两种方式:

  1. Use Postman to send post request. 使用邮递员发送发帖请求。
  2. Follow Spring-rest-client to create a rest client for test. 按照Spring-rest-client创建一个rest客户端进行测试。

You can test with Postman a great chrome extension tool for REST based APIs. 您可以使用Postman测试出色的chrome扩展工具,用于基于REST的API。 Run your server and hit the request from Postman. 运行您的服务器,然后按Postman的要求。 The image shows how to make a request. 该图显示了如何发出请求。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM