ExpediaGroup / jenkins-spock

Unit-test Jenkins pipeline code with Spock
https://javadoc.io/doc/com.homeaway.devtools.jenkins/jenkins-spock
Apache License 2.0
186 stars 73 forks source link

Not able to mock readJSON #132

Open hardikvjoshi opened 1 year ago

hardikvjoshi commented 1 year ago

Expected Behavior

def resultJSON = ''

try { resultJSON = readJSON text: scriptResult?.trim() } catch (ex) { echo 'The response is may not be in JSON format/No error received. \n Exception received trying to parse result as JSON : ' + ex }

I am trying to mock the readJSON and get resultJSON populated with some value.

String resTxt = "testString" String resTxt = "{'test':'value'}"

getPipelineMock("readJSON").call(["[text:'test value']"]) >> { return resTxt}

or

getPipelineMock("readJSON").call(["{'test':'value'}"]) >> { return resTxt}

or any other value even line below....

getPipelineMock("readJSON").call([[text:null]]) >> { return resTxt}

should be returning a value provided with return statement or should throw an exception.

Actual Behavior

Below block is seen anyways. And nothing is returned.

17:01:32.221 [main] DEBUG com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification - TEST FIXTURE intercepted & redirected a call: Test : class com.homeaway.devtools.jenkins.testing.JenkinsPipelineSpecification Note : pipeline step Via : queryWebUrl@6917bb4.methodMissing (types) : queryWebUrl.methodMissing Invocation : queryWebUrl@6917bb4.readJSON([[text:null]]) (types) : queryWebUrl.readJSON(Object[]) Forwarded To : Mock for type 'Closure' named 'getPipelineMock("readJSON")'.call([text:null]) (types) : Closure$$EnhancerByCGLIB$$94f4fb1c.call(LinkedHashMap)

coopernetes commented 1 year ago

The mocking will look for an exact match of the arguments passed in. It has to be a map (AFAIK), not a string-ified object such as your first two examples. Here's an example that I have working with a readJSONmock.

getPipelineMock("readJSON")([text: 'contents-of-scriptResult']) >> mockReturnValue