Skip to content

Commit

Permalink
Merge pull request #465 from nestoracunablanco/bugfix/mockOnDeclarative
Browse files Browse the repository at this point in the history
Bugfix: mock existing local function.
  • Loading branch information
nre-ableton committed Jan 7, 2022
2 parents 908129c + bb1a3e4 commit 8906c47
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class InterceptingGCL extends GroovyClassLoader {
Map.Entry<MethodSignature, Closure> matchingMethod = helper.allowedMethodCallbacks.find { k, v -> k == signature }
if (matchingMethod) {
// a matching method was registered, replace script method execution call with the registered closure (mock)
metaClazz."$scriptMethod.name" = matchingMethod.value
metaClazz."$scriptMethod.name" = matchingMethod.value ?: {}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.lesfurets.jenkins.unit.declarative

import org.junit.Before
import org.junit.Test

class TestMockLocalFunction extends DeclarativePipelineTest {

@Before
@Override
void setUp() throws Exception {
scriptRoots += 'src/test/jenkins/jenkinsfiles'
helper.registerAllowedMethod("githubNotify", [Map])
super.setUp()
}

@Test(expected=MissingMethodException.class)
void should_execute_with_errors() {
runScript("Mock_existing_function_Jenkinsfile")
}

@Test
void should_execute_without_errors() {
helper.registerAllowedMethod("runFunc")
runScript("Mock_existing_function_Jenkinsfile")
}
}
16 changes: 16 additions & 0 deletions src/test/jenkins/jenkinsfiles/Mock_existing_function_Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
pipeline {
agent { node { label 'test1' } }
stages {
stage('sdfsdf') {
steps {
githubNotify status: 'PENDING', context: 'Pipeline'
runFunc()
}
}
}
}

def runFunc(){
echo "inside runFunc"
doSomeStuff()
}

0 comments on commit 8906c47

Please sign in to comment.