# BaseGlobalVarTest

This abstract class serves as a base for writing unit tests for global Jenkins variables in Groovy scripts. It extends `BasePipelineTest` from the `com.lesfurets.jenkins.unit` package and provides utility methods to simplify testing.

## Utility Methods

### `getMethodCalls(String methodName)`

This method returns a list of method calls with the given method name that have been registered during the test execution.

### `getMethodCallArguments(String methodName, Integer callIndex = 0)`

This method retrieves the arguments of a method call with the specified method name and call index.

- `methodName`: The name of the method to retrieve arguments for.
- `callIndex`: The index of the method call to retrieve arguments from. Defaults to 0.

### `getMethodCallArgumentsAsString(String methodName, Integer callIndex = 0)`

This method retrieves the stringified arguments of a method call with the specified method name and call index.

- `methodName`: The name of the method to retrieve arguments for.
- `callIndex`: The index of the method call to retrieve arguments from. Defaults to 0.

### `getMethodCallArgument(String methodName, String argName, Integer callIndex = 0)`

This method retrieves the argument value of a method call with the specified method name and call index.

- `methodName`: The name of the method.
- `argName`: The name of the argument to retrieve value for.
- `callIndex`: The index of the method call to retrieve argument from. Defaults to 0.

### `assertMethodCalledTimes(String methodName, Integer numCalls)`

This method asserts that a method with the specified name has been called a certain number of times.

- `methodName`: The name of the method to assert calls for.
- `numCalls`: The expected number of calls.

### `assertMethodCalledNthWith(String methodName, callIndex: Integer, List<String> stringMatches)`

This method asserts that a method with the specified name has been called nth with arguments containing specific string matches.

- `methodName`: The name of the method to assert calls for.
- `callIndex`: The index of the method call to assert arguments for.
- `stringMatches`: A list of strings that should be contained in the method arguments.

### `assertMethodCalledNthWith(String methodName, callIndex: Integer, Map args)`

This method asserts that a method with the specified name has been called nth with specific arguments.

- `methodName`: The name of the method to assert calls for.
- `callIndex`: The index of the method call to assert arguments for.
- `args`: A map of arguments that should be contained in the method arguments.

### `assertMethodCalledNthWith(String methodName, callIndex: Integer, String stringMatch)`

Overloaded version of `assertMethodCalledNthWith` to assert a single string match.

### `assertMethodCalledWith(String methodName, Map args)`

This method asserts that a method with the specified name has been called with arguments containing specific matches.

- `methodName`: The name of the method to assert calls for.
- `args`: A map of arguments that should be contained in the method arguments.

### `assertMethodCalledWith(String methodName, List<String> stringMatches)`

This method asserts that a method with the specified name has been called with arguments containing specific string matches.

- `methodName`: The name of the method to assert calls for.
- `stringMatches`: A list of strings that should be contained in the method arguments.

### `assertMethodCalledWith(String methodName, String stringMatch)`

Overloaded version of `assertMethodCalledWith` to assert a single string match.

### `assertMethodCalledOnceWith(String methodName, List<String> stringMatches)`

This method asserts that a method with the specified name has been called exactly once and with arguments containing specific string matches.

- `methodName`: The name of the method to assert calls for.
- `stringMatches`: A list of strings that should be contained in the method arguments.

### `assertMethodCalledOnceWith(String methodName, Map args)`

This method asserts that a method with the specified name has been called exactly once and with specific arguments.

- `methodName`: The name of the method to assert calls for.
- `args`: A map of arguments that should be contained in the method arguments.

### `assertMethodCalledOnceWith(String methodName, String stringMatch)`

Overloaded version of `assertMethodCalledOnceWith` to assert a single string match.

### `assertThrowsWithMessage(Class exceptionClass, String expectedMessage, Closure func)`

This method asserts that the given Closure throws an exception of the given type and that the error message contains the expected string.

If the provided closure invokes a script, ensure you explicitly call the call() method on the script object. 
In Groovy 2.x, invoking a script directly (e.g., `script()`) does not work within a closure.

- `exceptionClass`: The class of the expected exception to be thrown.
- `expectedMessage`: A string that the error message should contain.
- `func`: The closure / function to execute.

## Usage

Extend this class in your unit test files for global Jenkins variables and utilize the provided utility methods to simplify the testing process.

### Example Usage

```groovy
import org.junit.jupiter.api.Test
import com.sonymusic.BaseGlobalVarTest

class MyGlobalVarTest extends BaseGlobalVarTest {

    @Test
    void testMethodCalls() {
        // Perform some actions that trigger global variable methods
        // ...

        // Assert that a method has been called a certain number of times
        assertMethodCalledTimes('methodName', 2)

        // Assert that a method has been called with specific arguments
        assertMethodCalledWith('methodName', [
            argument1: 'value1',
            argument2: 'value2'
        ])

        // Assert that a method has been called exactly once with specific arguments
        assertMethodCalledOnceWith('methodName', [argument1: 'value1'])

        // Assert that a method has been called with specific string matches
        // Note that it will match do a "containing" string match.
        assertMethodCalledWith('methodName', [
            'argument1=value1',
            'argument2=value2'
        ])

        // Assert that a method has been called exactly once with specific string matches
        assertMethodCalledOnceWith('methodName', ['argument1=value1'])
    }
}
```
