Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8386191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:52:04+00:00 2026-06-09T17:52:04+00:00

How can I test for a function that throws an expected exception? Here is

  • 0

How can I test for a function that throws an expected exception? Here is the function that throws the exception:

(defn seq-of-maps?
  "Tests for a sequence of maps, and throws a custom exception if not."
  [s-o-m]
  (if-not (seq? s-o-m)
    (throw (IllegalArgumentException. "s-o-m is not a sequence"))
    (if-not (map? (first s-o-m))
      (throw (IllegalArgumentException. "s-o-m is not a sequence of maps."))
      true)))

I want to design a test like the following in which an exception is thrown and caught and then compared. The following does not work:

(deftest test-seq-of-maps
  (let [map1  {:key1 "val1"}
        empv  []
        s-o-m (list {:key1 "val1"}{:key2 "val2"})
        excp1 (try 
                (seq-of-maps? map1)
                (catch Exception e (.getMessage e)))]
    (is (seq-of-maps? s-o-m))
    (is (not (= excp1 "s-o-m is not a sequence")))))

I am getting these errors:

Testing util.test.core

FAIL in (test-seq-of-maps) (core.clj:27)
expected: (not (= excp1 "s-o-m is not a sequence"))
  actual: (not (not true))

Ran 2 tests containing 6 assertions.
1 failures, 0 errors.    

Obviously, I am missing something about writing tests. I am having trouble figuring that out. My project was set up with lein new, and I am running the tests with lein test.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T17:52:06+00:00Added an answer on June 9, 2026 at 5:52 pm

    The last assertion in your test is incorrect; it should be (is (= excp1 "s-o-m is not a sequence")) since map1 isn’t a seq of maps.

    Aside from that, it’s probably clearer to use (is (thrown? ..)) or (is (thrown-with-msg? ...)) to check for thrown exceptions.

    Example:

    (t/is (thrown? java.lang.ClassCastException (s/valid? ::sut/int-value "x"))))
    

    Note that you just write classname as a symbol. Use java.lang.Exception if you don’t care about the exact Exception.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As topic suggested, how can I unit test a function that does not return
So I need a function to test if an image can load or not.
Can anyone tell me why this code would not be working? $('body').on('test', function() {
I am wanting to test a function on one of my models that throws
I have a VSTO Excel workbook that functions correctly in test. I can deploy
suppose I have a function: function test(){} test.prototype.method01=function(){ //do something } test.prototype.method02=function(){ //how can
I have a feature that uses the camera so I can test it only
This is a function that I want to write a unit test for: public
I have a set of tests, and I want to test that my classes
I am creating test.js from Java, as per below. Test.js implements function d(), that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.