r/programming • • Feb 22 '15

Java Makes Programmers Want To Do Absolutely Anything Else With Their Time

http://www.forbes.com/sites/quora/2015/02/21/java-makes-programmers-want-to-do-absolutely-anything-else-with-their-time/
35 Upvotes

636 comments sorted by

View all comments

Show parent comments

1

u/llogiq Feb 23 '15

It may be a bit more work (although given a good IDE, it's but a few keystrokes), but on the plus side, I get a unit test. :-)

1

u/yogthos Feb 23 '15

I get unit tests in Clojure as well, I just don't need nearly as many since the code is stateless. Also recommend looking at this. With Clojure, I can trivially mock any function in a test without having the code being mocked to be designed with that in mind.

For example, let's say there's a function, and it doesn't even have to be in my own code, could be a function from a library I'm using.

(defn get-user [connection id]
  (jdbc/query connection ["select * from users where id = ?" id]))

Now I'm doing unit tests and I want to have a mock user instead of checking the database:

(testing "get-user"
  (with-redefs [get-user (fn [_ _] {:id "bob" :pass "fake"})]
    (is (= {:id "bob" :pass "fake"} (get-user)))))

It's like monkey patching, but safe and locally scoped.

1

u/llogiq Feb 23 '15

I don't care much about mocking. During my time in consulting in an enterprise development scenario, I had to use it on occasion, but it was neither sufficiently awesome nor sufficiently painful to leave much of an impression.

1

u/yogthos Feb 23 '15

Mocking is just an example of how with-redefs can be useful.