Home > docs > plugins v2 > Mocks Task
Mocks plugin allow you to:
Mocks help isolate individual components during testing, making tests faster, safer, and more focused.
To be able to use the task in a Concord flow, it must be added as a dependency:
configuration:
dependencies:
- mvn://com.walmartlabs.concord.plugins.basic:mock-tasks:2.20.0
You can mock specific tasks to simulate their behavior.
flows:
main:
- task: myTask
in:
param1: "value"
out: taskResult
mainTest:
- set:
mocks:
# Mock the myTask task call
- task: "myTask"
out:
result: 42
- call: main
out: taskResult
- log: "${taskResult}" # prints out 'result=42'
In mainTest
, we set up a “mock” for the myTask
task. This mock intercepts calls to any myTask
instance and overrides the output, setting the result to 42
instead of running the actual task.
flows:
main:
- task: myTask
in:
param1: "value"
out: taskResult
mainTest:
- set:
mocks:
# Mock the myTask task call
- task: "myTask"
in:
param1: "value.*" # regular expression allowed for values
out:
result: 42
- call: main
out: taskResult
- log: "${taskResult}" # prints out 'result=42'
In mainTest
, we set up a mock to only intercept myTask
calls where param1 matched with regular
expression “value.*
”. When these parameter match, the mock replaces the task’s output with
result: 42
In addition to mocking entire tasks, you can also mock specific methods of a task.
flows:
main:
- expr: ${myTask.myMethod()}
out: taskResult
mainTest:
- set:
mocks:
# Mock the myTask task call
- task: "myTask"
method: "myMethod"
result: 42
- call: main
out: taskResult
- log: "${taskResult}" # prints out 'result=42'
In mainTest
, we set up a mock to only intercept myTask.myMethod
calls.
When these parameter match, the mock replaces the task’s output with result: 42
flows:
main:
- expr: ${myTask.myMethod(1)}
out: taskResult
mainTest:
- set:
mocks:
# Mock the myTask task call
- task: "myTask"
args:
- 1
method: "myMethod"
result: 42
- call: main
out: taskResult
- log: "${taskResult}" # prints out 'result=42'
In mainTest
, we set up a mock to only intercept myTask.myMethod
calls with input argument 1
.
When these parameter match, the mock replaces the task’s output with result: 42
flows:
main:
- expr: ${myTask.myMethod(1, 'someComplexVariableHere')}
out: taskResult
mainTest:
- set:
mocks:
# Mock the myTask task call
- task: "myTask"
args:
- 1
- ${mock.any()} # special argument that matches any input argument
method: "myMethod"
result: 42
- call: main
out: taskResult
- log: "${taskResult}" # prints out 'result=42'
In mainTest
, we set up a mock to only intercept myTask.myMethod
calls with input argument 1
and any
second argument. When these parameter match, the mock replaces the task’s output with
result: 42
The verify
task allows you to check how many times a specific task
(not necessarily a mocked task) with specified parameters was called.
flows:
main:
- task: "myTask"
out: taskResult
mainTest:
- call: main
- expr: "${verify.task('myTask', 1).execute()}"
In mainTest
, we verify that the myTask
task was called exactly once without input parameters
flows:
main:
- expr: ${myTask.myMethod(1)}
out: taskResult
mainTest:
- call: main
- expr: "${verify.task('myTask', 1).myMethod(1)}"
In mainTest
, we verify that the myMethod
method of the myTask
task was called exactly once
with a parameter 1
.