-1

I have written REST API which is between jasper server and my application. API is basically doing 3 things 1. Authenticating on jasper server. 2. Taking all present report list from jasper server repo to my application. 3. running the reports.
I need to write junit test cases using mockito for this API. I am using RESTEasy client.

Please help i am new to junit and mockito. Please provide if someone have good example on it. Thanks in advance.

2 Answers2

0

When you are writing unit tests you typically want to mock the external systems that you are interfacing with rather than actually interface to the real thing. Otherwise your unit tests are reliant on the external system being up and running.

What you mock depends on your own code and what interface it has to that external system. With RESTEasy perhaps you would have something like this:

    String expectedResponseFromJasperServer = ".....";

    ClientRequest request = mock(ClientRequest.class);
    ClientResponse<String> response = mock(ClientResponse.class);   
    when(request.get(String.class)).thenReturn(response);
    when(response.getEntity()).thenReturn(expectedResponseFromJasperServer);

This will then mock the response that comes back from the server to whatever you want it to be and also so that you can ensure your code handles that specific response.

jtsnr
  • 1,180
  • 11
  • 18
0

JASPERSERVER CE source code(which are public) contains a lot of JUnit tests includin remote REST tests. There are in the jasperserver-remote-tests module. There is an environment for testing REST using remote client.

Also you can do some search int the jasperserver-jax-rs-rest module. Where are a lot of unit tests, but if you have PRO jasperserver source code you can see pro REST modules.