Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Lesson Learned: Don't Use Low-Level Lib To Test High-Level Code

DZone's Guide to

Lesson Learned: Don't Use Low-Level Lib To Test High-Level Code

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

Summary: Using a fake http library to test logic two levels above HTTP is unnecessarily complex and hard to understand. Fake instead the layer directly below the logic you want to test and verify the low-level HTTP interaction separately. In general: Create thin horizontal slices for unit testing, checking each slice separately with nicely focused and clear unit tests. Then create a coarse-grained vertical (integration-like) test to test across the slices.

The case: I want to test that the method login sends the right parameters and transforms the result as expected. Login invokes post-raw which calls an HTTP method. Originally I have tried to test it by using the library clj-http-fake but it proved to be unnecessarily complex. It would be much better to fake post-raw itself for testing loginand test the original post-raw and its HTTP interaction separately, using that library.

There are two problems with using the low-level clj-http-fake library for testing the login function:

  1. Both the calls I need to make need to be wrapped in its with-fake-routes, which makes the test more complex.
  2. More seriously, I need to drop down from the level of Clojure data structures to JSON encoded as a String. The transformations between the two would pollute the test and be meaningless from the point of view of what I want to test.

Solution: Run instead login with a fake version of post-raw and, if desirable, test post-raw with clj-http-fake separately.

Pseudocode:

// "Business" logic
function login(..)
   authorizationToken = post-raw(...)
   return (new function derived from post-raw with some parameters such as auth.token bound)
 
// Test
...
fake post-raw(request) to return "my-authorization-token"
var post-authenticated = login(..)
assert post-authenticated is function
fake post-raw(request) to return request
assert post-authenticated(dummy-values) = {dummy-values + the auth. params}
// Testing post-raw isn't important now

See the clj-zabbix-api‘s core_test.clj for the full Clojure test and core.clj for the code. Disclaimer: It is not very good Clojure code. I am just learning :-)

Are you using Bitbucket to accomplish your company's mission? Share your company’s mission with #Forthecode for a chance to be featured on our homepage, our social media channels, or win a free t-shirt!

Topics:

Published at DZone with permission of Jakub Holý, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}