A Controller consist of two main parts:
- Mapped URL
- Code inside method
When writing a test case there is two parts to test.
- Testing URL’s and mapping
- Testing the code inside the function after mapping
Using classical test cases, using mock HttpServletResponse etc., using RESTtemplates (instead of using curl) are considerable things to use.
Question is that: Which one to use, how to use them, how to combine them and does it require logically separate that two test parts?
What kind of pattern to use to test a RESTful Service.
First of all:
So when I write tests I try to write them as easy as possible but also try to test every thing that I belive that I can go wrong.
So for example your Restfull Services:
Of course there can be many bugs in the logic, so one need to write a lot of tests for them. But this tests can directly invoke the method and so that its relativ easy to write the tests. Much more easyer than invoking the methods throug the some HTTP invokation.
But of course there can be an mistake in the HTTP invation so that must be tested to. But because I have already tested the “code” with my normal Unit tests. I only need not so many of that hard to write test. And this Test focus more on the bugs that can be done by wrong mapping or wrong serilization.
So at the end: Use all kind of tests that you can write (Unit Test, Mocked Stuff, HttpUnit test, Selenium,…). But first to think about the problem you want to test. And the choose the kind of test where you can write and maintain that test case easy.
Btw @see RestFuse – JUnit extension