Skip to content

Unable to mock a method from the script #108

@obrejla

Description

@obrejla

Hi!
Let's say I have a whatever.groovy script like this:

#!groovy
def call() {
    echo 'Some steps...'
    foo('bar')
}

def foo(param) {
    echo param
}

And what I want to do is to mock the foo method and check, whether it was called or not when call() method was called. I.e. testfile looks like this:

class WhateverTest extends BasePipelineTest {

    def whatever

    @Before
    void setUp() {
        super.setUp()
        whatever = loadScript('vars/whatever.groovy')
    }

    @Test
    void testFooCalled() throws Exception {
        // here somehow mock a `foo` method...following obviously does not work:
        helper.registerAllowedMethod('foo', [String.class], null)

        whatever()
        
        // here somehow check that it was called...following does not work:
        assertTrue(helper.callStack.findAll { call ->
            call.methodName == 'foo'
        }.any { call ->
            callArgsToString(call).contains('bar')
        })
    }
}

I also tried to incorporate Mockito, but no success in this case.

Am I doing something wrong? Or is there a bug?

Thanks...I'm desperate ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions