4

I have REST web service which needs to be tested. I am using Mockito for mocking DAO classes and RestAssured for testing REST methods through URI. Is there any way to test REST service without running it separately with Tomcat? Or how to run application on Tomcat with mocked classes before test cases?

Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114

1 Answers1

3

There is a tutorial that shows you how to use maven to start an embedded instance of tomcat and run tests against your service using RestAssured:

http://www.hascode.com/2011/09/rest-assured-vs-jersey-test-framework-testing-your-restful-web-services/

You start tomcat in one shell and run your tests in another.

However, I strongly recommend using the jersey test framework which transparently spins up an embedded container. In this case you wouldn't use RestAssured at all, but the jersey test client. Your tests will run more quickly and with less fuss. It's well documented here: https://jersey.github.io/documentation/latest/test-framework.html. The tutorial also demonstrates this approach, though it doesn't seem to me that the client is correctly constructed.

In the past I've also tested REST resources by calling the implementing class methods directly. Though this doesn't test the correct mapping of the http query parameters/body to java method parameters, it was often sufficient (especially when I'm also coding the client side code).

Marco Comi
  • 19
  • 1
  • 7
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • It worked, thanks. Left only 1 problem - ExceptionMapper is not invoked while Jersey Test. – Justinas Jakavonis Mar 30 '16 at 11:42
  • I'd open another question on the subject. – Robert Moskal Mar 30 '16 at 14:30
  • 1
    Already solved: http://stackoverflow.com/questions/36308419/jersey-test-exceptionmapper-is-not-invoked/36308529#36308529. Just needed to register my ExceptionMapper in public ResourceConfig configure(). – Justinas Jakavonis Mar 30 '16 at 14:35
  • Karate has support for REST testing without running Tomcat and the best part is that the same tests are re-usable as normal HTTP integration tests: https://github.com/intuit/karate/tree/master/karate-mock-servlet – Peter Thomas Sep 02 '17 at 11:36