Mysty
Mysty

Reputation: 83

writing Test cases for restful service using junit

i have been given a project to write test cases for restful service using junit. The restful service provides json data as output.I am new to this, although i have attained basic knowledge but still do not know what to test.Please help.

Upvotes: 0

Views: 4627

Answers (2)

John B
John B

Reputation: 32969

Ok. So there are several things that could / should be done.

  1. Unit test the Controller directly. Mock all other classes.
  2. Attempt to use any framework provided testing utilities to test how the controller interacts with the framework. An example of this is Spring's MockMVC. For an example, check out the "correct" answer to this question: JUnit test for ExceptionHandler

Both of the above to not require a deployed service to test and so work well in the test phase of maven.

  1. Additionally, although this would be an integration test, you could write JUnit tests using RestTemplate to hit the running service and verify the results. Since this has the external dependancy that the service is deployed and doesn't use any mocks it is an integration test. That said, it is a very valid set of tests and can be written in JUnit. However, these tests should be named (or categorized) as Integration tests so that they can be isolated from the tests that do not have external dependancies.

The fact that the service provides JSon should not be a big deal. Just use Jackson to unmarshal it into a domain object and verify the values are what you expect.

Upvotes: 1

Stefan Birkner
Stefan Birkner

Reputation: 24550

Send request and test the response. You may use restfuse.

Upvotes: 0

Related Questions