无法在 Jenkins 共享库的单元测试中导入包

Cannot import package in unit tests for a Jenkins Shared Library

我正在尝试使用 Gradle 为 JenkinsShared 库创建单元测试,以便 运行 测试任务。

我遵循了 this 教程,该教程得出结论,在 vars 文件夹中有一个用于函数共享库的工作测试套件(单元测​​试在 src/test/groovy/*Test.groovy 中) .

但是,在我们的内部共享 jenkins 库中,我们采用了更加面向对象的风格并将功能隔离到 classes 包中,格式为:src/org/company/*.groovy

尝试将所述包导入单元测试时出现问题 class。在本教程中,函数是使用 loadScript 方法导入的,此方法在加载依赖于另一个文件的 class 时失败。

取class:

package tests

import org.junit.*
import com.lesfurets.jenkins.unit.*
import static groovy.test.GroovyAssert.*

import org.company.UtilFactory

class UtilFactoryTest extends BasePipelineTest {
    @Test
    void testCall() {
        def util = UtilFactory.getUtil("hello")
        assertEquals true, true
    }
}

src/org/company/UtilFactory.groovy

package org.company

class UtilFactory implements Serializable {
    static Util instance   

    static Util getUtil(script=null) {
        if (!(UtilFactory.instance)) {
            if (!script) {
                // Throws an exception if on the first call to getUtil the 
                // script parameter is null.
                throw new ScriptUndefinedException("script parameter null on initial call to getUtil")
            }

            UtilFactory.instance = new Util(script)
        }
        return UtilFactory.instance
    }
}

class ScriptUndefinedException extends Exception {
    // Parameterless Constructor
    public ScriptUndefinedException() {}

    // Constructor that accepts a message
    public ScriptUndefinedException(String message)
    {
        super(message);
    }
}

这给了我例外:

jenkins-utilities/src/test/groovy/UtilFactoryTest.groovy: 7: 
unable to resolve class org.company.UtilFactory
    @ line 7, column 1.
    import org.company.UtilFactory

与 JenkinsShared Library 相比,这可能更像是一个 Gradle 问题。我刚刚花了一天的大部分时间试图弄清楚我做错了什么但无济于事。

我非常感谢任何帮助指导我朝着正确的方向前进。

此库可能有助于让您的共享库在单元测试中正常工作https://github.com/stchar/pipeline-sharedlib-testharness