在我的 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包含头:
(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运行测试。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*)))
))))