boot-clj: how to build/launch unit test for class generated with gen-class

231 views Asked by At

In my clojure project I built several java classes using gen-class command. They are [extractor.yaml YAMLExtractor YAMLExtractorFactory]. I wanted now build unit test against those classes but I have error: java.lang.ClassNotFoundException: extractor.yaml.YAMLExtractor when I run test.

File which cause the error is: yaml_extrator_factory.clj

(ns extractor.yaml-extractor-factory
  (:gen-class :name extractor.yaml.YAMLExtractorFactory
              :extends org.apache.any23.extractor.SimpleExtractorFactory
              :implements [org.apache.any23.extractor.ExtractorFactory]
              :init init
              :constructors {[] [String org.apache.any23.rdf.Prefixes java.util.Collection String]}
              :main false)
  (:require [extractor.yaml-extractor])
  (:import [extractor.yaml YAMLExtractor]
           [org.apache.any23.rdf Prefixes]
           [org.apache.any23.extractor SimpleExtractorFactory ExtractorFactory Extractor ExtractionContext ]))

The error occurs only during testing. Whole project is AOT-compilable with no error and it is fine when I build a jar file as well.

The test simple.clj contains head:

(ns extractor.simple
    (:use [clojure.tools.logging :as log]
          [clojure.java.io :as jio]
          [clojure.test :as test])
    (:require [extractor.yaml-extractor-factory])
    (:import [java.util Arrays]
             [extractor.yaml.YAMLExtractorFactory]))

And test which prints CLASSPATH. yaml-extractor-factory was not used. Test is run with command:

boot aot -a update-classpath run-test -t extractor.simple

where task update-classpath adds (get-env :directories) into classpath and run-test runs a test. run-test works fine with normal clojure code.

run-test is my task with following content:

(deftask run-test "Run unit tests"
  [t test-name NAME str "Test to execute. Run all tests if not given."]

  (require '[extractor.simple])
  (with-pass-thru [_]
    (if (nil? test-name)
      (do
        (util/info "Run all tests")
        (test/run-all-tests))
      (do
        (util/info (format "Run test: %s" (:test-name *opts*)))
        (test/run-tests (symbol (:test-name *opts*)))
        ))))
0

There are 0 answers