boot-clj:如何为使用gen-class生成的类构建/启动单元测试

时间:2016-12-20 23:58:07

标签: unit-testing clojure boot-clj gen-class clojure.test

在我的clojure项目中,我使用 gen-class 命令构建了几个java类。他们是[extractor.yaml YAMLExtractor YAMLExtractorFactory]。我现在想要针对这些类构建单元测试,但是当我运行测试时出现错误: java.lang.ClassNotFoundException: extractor.yaml.YAMLExtractor

导致错误的文件是: 的 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 ]))

仅在测试期间发生错误。整个项目是AOT可编译的,没有错误,当我构建一个jar文件时也没问题。

测试 simple.clj 包含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]))

测试打印CLASSPATH。 yaml-extractor-factory未使用。 使用命令运行测试:

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

其中任务 update-classpath (get-env :directories)添加到类路径中 并且运行测试运行测试。 run-test可以正常使用正常的clojure代码。

运行测试是我的任务,内容如下:

(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 个答案:

没有答案