【问题标题】:Cannot import package in unit tests for a Jenkins Shared Library无法在 Jenkins 共享库的单元测试中导入包
【发布时间】:2018-10-10 22:50:03
【问题描述】:

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

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

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

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

上课:

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

这可能更像是 Gradle 问题,而不是 JenkinsShared 库。我刚刚花了我一天的大部分时间试图弄清楚我到底做错了什么,但无济于事。

如果能帮助我朝着正确的方向前进,我将不胜感激。

【问题讨论】:

    标签: jenkins gradle groovy jenkins-pipeline jenkins-shared-libraries


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2020-04-16
      相关资源
      最近更新 更多