0

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. I need some guides to follow .

@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.

rahul
  • 1
  • What kind of test? Your options range from testing actual http responses using a http client over calling those methods like regular java methods to just testing the business logic used inside them. http://stackoverflow.com/questions/121266/unit-testing-a-jax-rs-web-service has some more info on that. – zapl Nov 24 '16 at 09:19
  • ya, I have to do unit testing for jax-rs service. Actually what are we going to do in test case is calling the actual function that we are going to do unit test, by giving random parameters appropriate to that function and get the return value and compare it with some constants that we already have in our test. Is this correct what i understood? – rahul Nov 24 '16 at 09:29

3 Answers3

0

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.

i.e.

public Response login(...)

In the test you can then make a new instance of the class and call the method normally.

e.g.

@Test
public void shouldDoSomething(){
  SomeClass someClass = new SomeClass();
  someClass.login(...);
  //some assertions or verifications
}
Farwatch
  • 46
  • 3
  • I changed to public, but still im not getting that method inside test – rahul Nov 24 '16 at 09:32
  • Response ssss = notificationManagementService.login(TestConstants.ENCODED_STRING, TestConstants.TENANT_ID); – rahul Nov 24 '16 at 09:49
  • It's hard to say exactly what the problem is without a bit more context. Could you add some more information about the NotificationManagementService and its Test class please? – Farwatch Nov 24 '16 at 13:49
0

Two ways:

  1. Use Postman to send post request.
  2. Follow Spring-rest-client to create a rest client for test.
Dave Pateral
  • 1,415
  • 1
  • 14
  • 21
0

You can test with Postman a great chrome extension tool for REST based APIs. Run your server and hit the request from Postman. The image shows how to make a request.

enter image description here

Atif Imran
  • 1,899
  • 2
  • 18
  • 33