开发者

unit testing restful webservices

开发者 https://www.devze.com 2023-04-08 04:29 出处:网络
I am wondering if anyone knows the proper way to unit test a restful webservice. I have a set of webservices built using recess, and I would like to write test code for them. Unfortunately, since my w

I am wondering if anyone knows the proper way to unit test a restful webservice. I have a set of webservices built using recess, and I would like to write test code for them. Unfortunately, since my webservices are tied to a database, my tests end up populating the database which seems like a problem.

I am mostly asking about the proper approach to dealing with this from a unit test standpoint. Do I clear the database of the values I have inserted after testing? Do I have a special test database with a whole set of special test routes? I am at a bit of a loss for the best way to approach this.

Obviously in other cases of similar database wrapper classes you would just pass in a dummy database that you set up at the beginning of the tests. This seems like it would be much more challenging though when it comes to working with a restful framework like recess.

I'd appreciate any thought you all might 开发者_开发问答have on the right way to deal with tests saving information to the database.

Thanks in advance.


Generally when testing a web service you are testing the full stack, from the outside in. This means you request a resource and check if the results conform to your expectations.

In nearly all cases populating the database right before every request is a good approach. It might seem like overkill, but in reality with a web service you can't guarantee proper test coverage by mocking/stubbing various elements.

Coming from the Ruby world, Cucumber is the ideal approach as it lets you test from a high level. When you combine this with Rspec to do actual unit testing (lower level tests that query your objects directly) you get the best of both worlds. These libraries even come with something called 'database cleaner' which will manage populating and depopulating the database for you.

You might find the following blog post by Rspec's author very helpful, as it explains brilliantly why you should avoid too much mocking and stubbing. http://blog.davidchelimsky.net/2011/09/22/avoid-stubbing-methods-invoked-by-a-framework/


Generally speaking you have two options:

1) Use a dedicated test database with known data on which you can set your expectations - replace the DB with a "pristine DB" before starting testing. This would be considered integration testing since you are in fact dependent on the database.

2.) Make your code independent of the actual data store and pass in the dependency to the persistence layer. For unit testing you can write (or mock out) a custom persistence layer/object that allows you to observe the state changes that your are unit testing.

A healthy mix of both depending on the scenario usually provides good coverage.

Also instead of testing your Restful web service consider just delegating to a POCO within each service endpoint and then just test these POCOs directly - much easier testable and all you have left to do is verifying the mapping between service endpoint and POCO.


My understanding is that if you do your tests in this order you can test all the verbs but there will be no additional data in the DB at the end.

POST ( add a new record)  
GET  ( fetch the newly added record)  
PUT/PATCH ( modify the newly added record)  
DELETE (delete the newly added record)  

Of course somebody else using the database at the same time might see transient values during the duration of the test.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号