Activiti User Guide -- Activit 用户指南 Part04
?
ProcessEngine processEngine = new ProcessEngineBuilder() .configureFromPropertiesResource(configurationResource) .buildProcessEngine();RuntimeService runtimeService = processEngine.getRuntimeService();RepositoryService repositoryService = processEngine.getRepositoryService();TaskService taskService = processEngine.getTaskService();ManagementService managementService = processEngine.getManagementService();IdentityService identityService = processEngine.getIdentityService();HistoryService historyService = processEngine.getHistoryService();?We've also added a couple of classes that can provideconvenience for unit testing processes in package?org.activiti.engine.test.
/** * Called when the task is successfully executed. * @param taskId the id of the task to complete, cannot be null. * @throws ActivitiException when no task exists with the given id. */ void complete(String taskId);?In the example above, when an id is passed for which notask exists, an exception will be thrown. Also, since the javadoc?explicitly states that taskId cannotbe null, an?ActivitiException?will be thrown when?null?is passed.
public class MyBusinessProcessTest extends ActivitiTestCase { @Deployment public void testSimpleProcess() { runtimeService.startProcessInstanceByKey("simpleProcess"); Task task = taskService.createTaskQuery().singleResult(); assertEquals("My Task", task.getName()); taskService.complete(task.getId()); assertEquals(0, runtimeService.createProcessInstanceQuery().count()); }} ?To get the same functionality when using the Junit 4style of writing unit tests, the?org.activiti.engine.test.ActivitiRule?Rule must be used. Through this rule, the process engineand services are available through getters. As with the?ActivitiTestCase?(see above), including this Rule will enable the use ofthe?org.activiti.engine.test.Deployment?annotation (see above for an explanation of its use andconfiguration). Process engines are statically cached over mutliple unit testswhen using the same configuration resource.
public class MyBusinessProcessTest { @Rule public ActivitiRule activitiRule = new ActivitiRule(); @Test @Deployment public void ruleUsageExample() { RuntimeService runtimeService = activitiRule.getRuntimeService(); runtimeService.startProcessInstanceByKey("ruleUsage"); TaskService taskService = activitiRule.getTaskService(); Task task = taskService.createTaskQuery().singleResult(); assertEquals("My Task", task.getName()); taskService.complete(task.getId()); assertEquals(0, runtimeService.createProcessInstanceQuery().count()); }}
?
PvmProcessDefinition processDefinition = new ProcessDefinitionBuilder() .createActivity("a") .initial() .behavior(new WaitState()) .transition("b") .endActivity() .createActivity("b") .behavior(new WaitState()) .transition("c") .endActivity() .createActivity("c") .behavior(new WaitState()) .endActivity() .buildProcessDefinition();PvmProcessInstance processInstance = processDefinition.createProcessInstance();processInstance.start();PvmExecution activityInstance = processInstance.findExecution("a");assertNotNull(activityInstance);activityInstance.signal(null, null);activityInstance = processInstance.findExecution("b");assertNotNull(activityInstance);activityInstance.signal(null, null);activityInstance = processInstance.findExecution("c");assertNotNull(activityInstance);?
?