针对Activiti6.0的formService的测试

FormService简介:

formService主要是用来在流程中处理表单内容的,在学习formService之后,发现启动方式除了调用runtimeService中的startProcessInstanceById 或者startProcessInstanceBykey等方法紫外还有formService的方法如下:

 //通过form服务启动
        Map<String, String> properties = Maps.newHashMap();
        properties.put("message", "this is my test message");
        ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), properties);

完成方法也不止有complete这一种了,还有下面这种方法:

        Map<String, String> properties1 =  Maps.newHashMap();
        properties1.put("yesOrNo", "yes");
        formService.submitTaskFormData(task.getId(), properties1);
        Task task1 = activitiRule.getTaskService().createTaskQuery().taskId(task.getId()).singleResult();
        LOGGER.info("task1 = {}", task1);

完整测试的xml和代码如下:
xml:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="my-process">
    <startEvent id="start" activiti:formKey="/rest/process/form/start">
        <!-- 扩展元素 -->
        <extensionElements>
            <activiti:formProperty id="message" name="信息" type="string" required="true"/>
        </extensionElements>
    </startEvent>
    <sequenceFlow sourceRef="start" targetRef="someTask" id="flow1"/>
    <userTask id="someTask" name="执行到我了" activiti:formKey="/rest/process/form/userTask">
        <extensionElements>
            <activiti:formProperty id="yesOrNo" name="审批" type="string" required="true"/>
        </extensionElements>
    </userTask>
    <endEvent id="end"/>
    <sequenceFlow id="flow2" sourceRef="someTask" targetRef="end"/>
</process>
</definitions>

测试类:

@Test
    @Deployment(resources = {"my-process-form.bpmn20.xml"})
    public void testFormService() {

        FormService formService = activitiRule.getFormService();
        ProcessDefinition processDefinition = activitiRule.getRepositoryService().createProcessDefinitionQuery().singleResult();

        String startFormKey = formService.getStartFormKey(processDefinition.getId());
        LOGGER.info("startFormKey = {}", startFormKey);

        StartFormData startFormData = formService.getStartFormData(processDefinition.getId());
        List<FormProperty> formProperties = startFormData.getFormProperties();
        formProperties.forEach(form -> {
            LOGGER.info("startFormKey = {}", ToStringBuilder.reflectionToString(startFormData, ToStringStyle.JSON_STYLE));
        });

        //通过form服务启动
        Map<String, String> properties = Maps.newHashMap();
        properties.put("message", "this is my test message");
        ProcessInstance processInstance = formService.submitStartFormData(processDefinition.getId(), properties);

        Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
        //流程启动才会确定taskId
        TaskFormData taskFormData = formService.getTaskFormData(task.getId());
        List<FormProperty> formProperties1 = taskFormData.getFormProperties();
        formProperties1.forEach(formProperty -> {
            LOGGER.info("formProperty = {}", ToStringBuilder.reflectionToString(formProperty, ToStringStyle.JSON_STYLE));
        });

        Map<String, String> properties1 =  Maps.newHashMap();
        properties1.put("yesOrNo", "yes");
        formService.submitTaskFormData(task.getId(), properties1);
        Task task1 = activitiRule.getTaskService().createTaskQuery().taskId(task.getId()).singleResult();
        LOGGER.info("task1 = {}", task1);
    }
}

动态表单的生成(未完待续)

猜你喜欢

转载自blog.csdn.net/qq_42046342/article/details/100897668