| career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (junit.clj)

This example Java source code file (junit.clj) is included in the "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

all, eclipse, hickey, jason, june, junit, junit-compatible, sankey, this, throwable, xml

The junit.clj Java example source code

;   Copyright (c) Rich Hickey. All rights reserved.
;   The use and distribution terms for this software are covered by the
;   Eclipse Public License 1.0 (
;   which can be found in the file epl-v10.html at the root of this distribution.
;   By using this software in any fashion, you are agreeing to be bound by
;   the terms of this license.
;   You must not remove this notice, or any other, from this software.

;; test/junit.clj: Extension to clojure.test for JUnit-compatible XML output

;; by Jason Sankey
;; June 2009


(ns ^{:doc "clojure.test extension for JUnit-compatible XML output.

  JUnit ( is the most popular unit-testing library
  for Java.  As such, tool support for JUnit output formats is
  common.  By producing compatible output from tests, this tool
  support can be exploited.

  To use, wrap any calls to clojure.test/run-tests in the
  with-junit-output macro, like this:

    (use 'clojure.test)
    (use 'clojure.test.junit)

      (run-tests '

  To write the output to a file, rebind clojure.test/*test-out* to
  your own PrintWriter (perhaps opened using"
  :author "Jason Sankey"}
  (:require [clojure.stacktrace :as stack]
            [clojure.test :as t]))

;; copied from clojure.contrib.lazy-xml
(def ^{:private true}
     (zipmap "'<>\"&" (map #(str \& % \;) '[apos lt gt quot amp])))
(defn- escape-xml [text]
  (apply str (map #(escape-xml-map % %) text)))

(def ^:dynamic *var-context*)
(def ^:dynamic *depth*)

(defn indent
  (dotimes [n (* *depth* 4)] (print " ")))

(defn start-element
  [tag pretty & [attrs]]
  (if pretty (indent))
  (print (str "<" tag))
  (if (seq attrs)
    (doseq [[key value] attrs]
      (print (str " " (name key) "=\"" (escape-xml value) "\""))))
  (print ">")
  (if pretty (println))
  (set! *depth* (inc *depth*)))

(defn element-content
  (print (escape-xml content)))

(defn finish-element
  [tag pretty]
  (set! *depth* (dec *depth*))
  (if pretty (indent))
  (print (str "</" tag ">"))
  (if pretty (println)))

(defn test-name
  (apply str (interpose "."
                        (reverse (map #(:name (meta %)) vars)))))

(defn package-class
  (let [i (.lastIndexOf name ".")]
    (if (< i 0)
      [nil name]
      [(.substring name 0 i) (.substring name (+ i 1))])))

(defn start-case
  [name classname]
  (start-element 'testcase true {:name name :classname classname}))

(defn finish-case
  (finish-element 'testcase true))

(defn suite-attrs
  [package classname]
  (let [attrs {:name classname}]
    (if package
      (assoc attrs :package package)

(defn start-suite
  (let [[package classname] (package-class name)]
    (start-element 'testsuite true (suite-attrs package classname))))

(defn finish-suite
  (finish-element 'testsuite true))

(defn message-el
  [tag message expected-str actual-str]
  (start-element tag false (if message {:message message} {}))
   (let [[file line] (t/file-position 5)
         detail (apply str (interpose
                            [(str "expected: " expected-str)
                             (str "  actual: " actual-str)
                             (str "      at: " file ":" line)]))]
     (if message (str message "\n" detail) detail)))
  (finish-element tag false)

(defn failure-el
  [message expected actual]
  (message-el 'failure message (pr-str expected) (pr-str actual)))

(defn error-el
  [message expected actual]
  (message-el 'error
              (pr-str expected)
              (if (instance? Throwable actual)
                (with-out-str (stack/print-cause-trace actual t/*stack-trace-depth*))
                (prn actual))))

;; This multimethod will override test-is/report
(defmulti ^:dynamic junit-report :type)

(defmethod junit-report :begin-test-ns [m]
   (start-suite (name (ns-name (:ns m))))))

(defmethod junit-report :end-test-ns [_]

(defmethod junit-report :begin-test-var [m]
   (let [var (:var m)]
     (binding [*var-context* (conj *var-context* var)]
       (start-case (test-name *var-context*) (name (ns-name (:ns (meta var)))))))))

(defmethod junit-report :end-test-var [m]

(defmethod junit-report :pass [m]
   (t/inc-report-counter :pass)))

(defmethod junit-report :fail [m]
   (t/inc-report-counter :fail)
   (failure-el (:message m)
               (:expected m)
               (:actual m))))

(defmethod junit-report :error [m]
   (t/inc-report-counter :error)
   (error-el (:message m)
             (:expected m)
             (:actual m))))

(defmethod junit-report :default [_])

(defmacro with-junit-output
  "Execute body with modified test-is reporting functions that write
  JUnit-compatible XML output."
  {:added "1.1"}
  [& body]
  `(binding [t/report junit-report
             *var-context* (list)
             *depth* 1]
       (println "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")
       (println "<testsuites>"))
     (let [result# (do ~@body)]
       (t/with-test-out (println "</testsuites>"))

Other Java examples (source code examples)

Here is a short list of links related to this Java junit.clj source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller


new blog posts


Copyright 1998-2021 Alvin Alexander,
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.