0

I have a set function that calls an API of another Service.

I implemented a feature where

  1. I retry the set() upto 3 times if the set() fails.
  2. If retry() fails for all the 3 times, cut a ticket (generate an email saying there is an error in set() ).

I want to test the above feature.

  1. Mock the set() such that I can verify retry works successfully.

  2. Mock set() and retry such that I can verify the creation of ticket.

How can I mock the above scenarios in clojure?

My function looks like this:

(defn set *
  "Set"
  [param1 param2 param3]
  (try
    (utils/retry 2
                 (do (set param1 param2 param3)))
    (catch Exception e
      (cut-ticket))))




(defmacro retry
  "Evaluates expr up to cnt + 1 times, retrying if an exception
  is thrown. If an exception is thrown on the final attempt, it
  is allowed to bubble up."
  [cnt expr]
  (letfn [(go [cnt]
            (if (zero? cnt)
              expr
              `(try ~expr
                    (catch Exception e#
                      (retry ~(dec cnt) ~expr)))))]
    (go cnt)))

Ref: retrying something 3 times before throwing an exception - in clojure

Community
  • 1
  • 1
Swagatika
  • 857
  • 1
  • 11
  • 32

1 Answers1

0

Start by reading about with-redefs: https://clojuredocs.org/clojure.core/with-redefs

This should solve question #1. I'm not sure you need to mock retry, just create a mock for cut-ticket

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • Thanks. cut-ticket creates a ticket, as in, it generates an email saying there is a problem. I want to verify that the email was generated. So I don't need to create a mock for cut-ticket. My goal is to test if I am successfully cutting the ticket (generating the email) or not. I hope that clarifies the 2nd question. – Swagatika May 04 '17 at 20:44