Task task = taskService.createTaskQuery().singleResult();
assertNotNull(task);
taskService.setVariableLocal(task.getId(), "testVar", "anotherTestValue");
ExecutionEntity taskExecution = (ExecutionEntity) runtimeService.createExecutionQuery().singleResult();
assertNotNull(taskExecution);
HistoricVariableInstanceQuery query = historyService.createHistoricVariableInstanceQuery();
assertEquals(2, query.count());
List<HistoricVariableInstance> result = query.list();
HistoricVariableInstance firstVar = result.get(0);
assertEquals("testVar", firstVar.getVariableName());
assertEquals("testValue", firstVar.getValue());
// the variable is in the process instance scope
assertEquals(pi.getId(), firstVar.getActivityInstanceId());
HistoricVariableInstance secondVar = result.get(1);
assertEquals("testVar", secondVar.getVariableName());
assertEquals("anotherTestValue", secondVar.getValue());
// the variable is in the task scope
assertEquals(taskExecution.getActivityInstanceId(), secondVar.getActivityInstanceId());
taskService.complete(task.getId());
assertProcessEnded(pi.getId());
}