Thursday, May 07, 2015

Use Caching Service as Mocked Microservices

For a long period of time, we've been using mocking framework to ease unit testing. The idea behind is,
  • in production, object A's method a() calls object B's method b()
  • in test, we
    • pre-define b()'s behaviour
    • inject mocked object B into object-under-test A
    • fire a() and verify the output
With the shift from monolithic style to microservices oriented software design, we call remote APIs (RESTful or RPC) more often than we did before. Quite a few HttpMock products were created to allow us to test remote APIs the way we used to test local APIs.

But if we have a real HTTP server somewhere, in order to mock response from remote, all we need is to set up endpoint with the response and later tear it down. As a side-product, caching service is such a HTTP server that can easily be used to mock remote APIs object-under-test consume. It works this way
  • in product, object A's method a() send HTTP request to service B's endpoint b
  • in test, we
    • pre-populate (POST) expected response in cache server for endpoint b
    • send (GET) HTTP request to it (instead of the production endpoint) from A.a()
    • get response from cache server and verify a()'s output
    • remove (DELETE) the response from cache server
Now you see how the idea of microservices may also change the way you test your microservice oriented production code.

Happy testing!

Thursday, April 09, 2015

Push specific local commit to remote

Like what's asked in this Stack Overflow question, sometimes we keep local commits for a while, amending commit message, merging multiple commits into a more meaningful one, trying something in a branch from unpublished commits and so on. And finally, we decide to push some oldest commits to remote. Here are the steps.
  1. Check in all changes locally

  2. Branch it (not only for backup, but also to keep commit history when resetting)
  3. git branch backup

  4. Reset to the oldest unpublished commit
  5. git reset --hard {sha of commit to push}

  6. Push it to remote
  7. git push origin {branch}

  8. Go to step 2 if you have more commit to push

  9. Reset to latest local commit
  10. git reset --hard {sha of latest commit}

  11. Delete backup branch
  12. git branch -d backup

Happy coding!