ProcessInstanceQuery processInstanceQuery = runtimeService.createProcessInstanceQuery();
// get process instance
ProcessInstance pi = processInstanceQuery.processDefinitionKey(key).singleResult();
VariableInstanceQuery variableInstanceQuery = getClearVariableInstanceQuery(pi.getId());
ExecutionQuery executionQuery = runtimeService.createExecutionQuery().processInstanceId(pi.getId());
TaskQuery taskQuery = taskService.createTaskQuery().processInstanceId(pi.getId());
JobQuery jobQuery = managementService.createJobQuery().processInstanceId(pi.getId());
Task task = taskQuery.singleResult();
// there should be five variable instance (3 process instance variables) and...
Assert.assertEquals(5, variableInstanceQuery.count());
// ...2 task variable
Assert.assertEquals(2, variableInstanceQuery.taskIdIn(task.getId()).count());
// add a new task variable
taskService.setVariableLocal(task.getId(), "aNewLocalTaskVariableName", "aNewLockTaskVariableValue");
// change task variable value
taskService.setVariableLocal(task.getId(), "aLocalTaskVariableName", "aNewValue");
Assert.assertEquals(3, variableInstanceQuery.taskIdIn(task.getId()).count());
// complete task
taskService.complete(task.getId());
variableInstanceQuery = getClearVariableInstanceQuery(pi.getId());
// there should be three variables...
Assert.assertEquals(3, variableInstanceQuery.count());
Execution execution = executionQuery.singleResult();
// update existing variable
runtimeService.setVariableLocal(execution.getId(), "anotherVariableName", "aNewFinalValue");