0

I have a groovy file gitClone.groovy which has a function call.

def call(credentials, url, project, branch, path, refs, noTags=false,
    timeout=20)
{

}

I am writing a test for validating the 'url'. I want to write a test case which will validate if the url is correct or not. My test file is as follows: gitCloneSpec.groovy

import com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification;
import spock.lang.*
import java.net.URL;
public class gitCloneSpec extends JenkinsPipelineSpecification {

        def gitClone = null
        def check = 0
        def setup() {
                gitClone = loadPipelineScriptForTest("vars/gitClone.groovy")
                gitClone.getBinding().setVariable( "url", "https://www.google.com/")
                                 
                }

        def "validate url"(){
                when:
                        try {
                                URL u  = new URL(url)
                                u.toURI()
                                check = 1
                        }

                        // If there was an Exception
                        // while creating URL object
                        catch (Exception e) {
                                check = 2;
                        }
                then:
                        check == 1
                }
}

Somehow url is not able to store the string "http://www.google.com" and it is throwing the exception where 'check' is getting updated with value '2' How can I perform this test?

Subhojoy Dey
  • 3
  • 1
  • 3
  • I bet if you print the exception, it's that `url` isn't defined when you do `URL u = new URL(url)`. I can't see what you are trying to do, nor how you want it to work :-/ – tim_yates Nov 30 '21 at 15:26
  • "How can I perform this test?" - Do you want to test the logic that is inside of your `call` method, do you want to test that the code that invokes your `call` method passes an expected value, or something else? – Jeff Scott Brown Nov 30 '21 at 16:25
  • I do not see that `url` gets initialised anywhere in your test. You set an identically named binding in your `gitClone` field, but not in the specification or feature method itself. – kriegaex Nov 30 '21 at 18:16
  • How do I initialise **url** I was assuming `gitClone.getBinding().setVariable( "url", "https://www.google.com/")` initialises url but maybe I was wrong. So, how do I initialize it. – Subhojoy Dey Dec 02 '21 at 04:04
  • My aim is to check if the `url` in the call function is a valid url. I don't wish to test anything else. – Subhojoy Dey Dec 02 '21 at 04:07
  • @kriegaex I tested the code by defining a variable `url` in my feature method `def url = "https://www.google.com/"` and it worked. But now how do I link it with the `url` of gitClone.groovy. I mean I want to test the `url` of function `call`, so how should I initialize it .Please help :) – Subhojoy Dey Dec 03 '21 at 05:16
  • I am not a Jenkins API user. So if you would please provide an [MCVE](https://stackoverflow.com/help/mcve) on GitHub which I can clone and build, I could take a look. – kriegaex Dec 05 '21 at 04:29

0 Answers0