• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best way to Unit Test Java Restful Web Service without Connecting to DB

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using JDK 1.7, Jersey 2.12, Tomcat 7, MongoDB and RestAssured < https://code.google.com/p/rest-assured/ > to unit test my Rest calls... However, I always have to my MongoDB daemon started along with the war file deployed to Tomcat 7 whenever I run my unit tests. Is there a solution where I can run unit tests without depending on Tomcat and MongoDB to test my Rest calls? So, basically an embedded server or in-memory server which can use mock objects to test my Restful Web Services? Would love to hear people's suggestions regarding best practices for this?

I also posted this to Stack Overflow:

http://stackoverflow.com/questions/25539666/best-api-lib-to-unit-test-jersey-restful-web-services
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When running tests that interacts with a database I usually try to use a in-memory database but in this case you are using Mongo so it's not quite that simple.

For some API tests you can use JUnit or maybe restfuse, an extension of JUnit. It works pretty good for simple stuff. Sample: http://blog.mintcube.solutions/2014/08/swift-api-testing-with-restfuse/

Regarding the way to make the process faster, if you are using Maven, can you run the maven build with something like those goals: 'clean install tomcat:run test'? (build your application, start the server and run the unit tests)

With mongo I would try one of the following
- look at Embedded MongoDB
- try to do some transaction rollback before putting data in Mongo
- clean up Mongo after the tests will run
reply
    Bookmark Topic Watch Topic
  • New Topic