Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to mock readJSON #132

Open
hardikvjoshi opened this issue Nov 23, 2022 · 1 comment
Open

Not able to mock readJSON #132

hardikvjoshi opened this issue Nov 23, 2022 · 1 comment

Comments

@hardikvjoshi
Copy link

hardikvjoshi commented Nov 23, 2022

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 : [email protected]
(types) : queryWebUrl.methodMissing
Invocation : [email protected]([[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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants