Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to write unit tests for a REST API?

When writing unit tests for an API wrapper, should I be making real calls to the REST API endpoints or should I use mocl responses that simulate successful and erroneous calls?

like image 268
Carson Avatar asked Jan 19 '13 21:01

Carson


1 Answers

Unit tests mean testing only your unit (API wrapper), nothing else. Thus, unfortunately, you should mock the entire API.

On the other hand this never really gives me enough confidence, so I go for system tests (also known as component tests). In that case you should run your API wrapper against existing API, maybe embedded and started along with your test. In integration test, the ultimate scenario, you run your API wrapper against real, but most likely test instance of the API (sandbox, dev environment).

In well established area of database testing: unit tests mock entire DAO level, component tests run against in-memory database while integration tests connect to real database with some fake data.

like image 176
Tomasz Nurkiewicz Avatar answered Oct 13 '22 10:10

Tomasz Nurkiewicz