Skip to content

How to mock object creation? #589

@Warchant

Description

@Warchant

Describe your use-case which is not covered by existing documentation.

Documentation lacks an example which shows how to mock object creation in the pipeline.

Specifically, my jenkins controller has this plugin installed: https://plugins.jenkins.io/influxdb/
And in my shared library I have a file:
/vars/dashboard.groovy with a function that is similar to this: https://github.com/jenkinsci/influxdb-plugin#via-jenkins-pipeline

def influxDb(Map args) {
    lock(targetName) {
        // Copy from: https://github.com/jenkinsci/influxdb-plugin#via-jenkins-pipeline
        def influxdb = Jenkins.instance.getDescriptorByType(jenkinsci.plugins.influxdb.InfluxDbStep.DescriptorImpl)
        def targests = influxdb.getTargets()
        influxdb.removeTarget(targetName)
        def target = new jenkinsci.plugins.influxdb.models.Target()
        target.description = targetName
        target.url = args.dashboard.influx_server_url

See this line: def target = new jenkinsci.plugins.influxdb.models.Target().

Because local gradle project does not define any dependency on this influxdb-plugin, this class is undefined. When I attempt to load my shared library, I get an error

file:/C:/W/jenkins_shared_library/test/resources/ReportTest_WhenUnstable.Jenkinsfile: 1: Error on loading library LuCI@master : startup failed:
C:\W\jenkins_shared_library\vars\dashboard.groovy: 101: unable to resolve class jenkinsci.plugins.influxdb.models.Target
 @ line 101, column 22.
           def target = new jenkinsci.plugins.influxdb.models.Target()

I'd like to instantiate a mock here.

Reference any relevant documentation, other materials or issues/pull requests that can be used for inspiration.

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions