Skip to content

Instantly share code, notes, and snippets.

@double16
Created January 15, 2019 18:33
Show Gist options
  • Save double16/6ba77c57e502c6972b81daa1c000ec0a to your computer and use it in GitHub Desktop.
Save double16/6ba77c57e502c6972b81daa1c000ec0a to your computer and use it in GitHub Desktop.
Gradle ExecAction mock for Spock tests
class MockExecAction extends DefaultExecAction {
int exitValue = 0
String output = ''
MockExecAction() {
super(null, null, null)
}
@Override
ExecResult execute() {
standardOutput.write(output.getBytes('UTF-8'))
ExecResult result = new ExecResult() {
@Override
int getExitValue() {
MockExecAction.this.exitValue
}
@Override
ExecResult assertNormalExitValue() throws ExecException {
if (exitValue != 0) {
throw new ExecException("Command exited with ${exitValue}: ${getCommandLine().join(' ')}")
}
this
}
@Override
ExecResult rethrowFailure() throws ExecException {
assertNormalExitValue()
}
}
result.assertNormalExitValue()
result
}
}
class MockExecActionFactory implements ExecActionFactory {
MockExecAction mockExecAction = new MockExecAction()
@Override
ExecAction newExecAction() {
mockExecAction
}
@Override
JavaExecAction newJavaExecAction() {
return null // TODO:
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment