翻看flowable源码测试类了解api发现每个测试方法都自定义注解@Deployment
@Test
@Deployment(resources = { "org/flowable/engine/test/bpmn/StartToEndTest.testStartToEnd.bpmn20.xml" })
public void testStartProcessInstanceWithVariables() {
Map<String, Object> varMap = new HashMap<>();
varMap.put("test", "hello");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("startToEnd", varMap);
assertProcessEnded(processInstance.getId());
Map<String, Object> returnVarMap = ((ExecutionEntity) processInstance).getVariables();
assertEquals("hello", returnVarMap.get("test"));
}
该注解的好处,不需要自己手动重复部署流程。查看注解源码:
@Retention(RetentionPolicy.RUNTIME)
public @interface Deployment {
/** Specify resources that make up the process definition. */
public String[] resources() default {};//注解地址
/** Specify tenantId to deploy for */
public String tenantId() default "";//租户id
}
那么他是如何扫描的呢??翻看继续追述源码 发现InternalFlowableExtension类,该类继承BeforeEachCallback并重写beforeEach方法:
@Override
public void beforeEach(ExtensionContext context) {
ProcessEngine processEngine = getProcessEngine(context);
//扫描含有@Deployment注解
AnnotationSupport.findAnnotation(context.getTestMethod(), Deployment.class)
.ifPresent(deployment -> {
//对含有@Deployment注解 进行流程部署
String deploymentIdFromDeploymentAnnotation = TestHelper
.annotationDeploymentSetUp(processEngine, context.getRequiredTestClass(), context.getRequiredTestMethod(), deployment);
getStore(context).put(context.getUniqueId() + ANNOTATION_DEPLOYMENT_ID_KEY, deploymentIdFromDeploymentAnnotation);
});
}
继续跟进TestHelper.annotationDeploymentSetUp
方法
public static String annotationDeploymentSetUp(ProcessEngine processEngine, Class<?> testClass, Method method) {
Deployment deploymentAnnotation = method.getAnnotation(Deployment.class);
return annotationDeploymentSetUp(processEngine, testClass, method, deploymentAnnotation);
}
public static String annotationDeploymentSetUp(ProcessEngine processEngine, Class<?> testClass, Method method, Deployment deploymentAnnotation) {
String deploymentId = null;
String methodName = method.getName();
if (deploymentAnnotation != null) {
LOGGER.debug("annotation @Deployment creates deployment for {}.{}", testClass.getSimpleName(), methodName);
String[] resources = deploymentAnnotation.resources();
if (resources.length == 0) {
String name = method.getName();
String resource = getBpmnProcessDefinitionResource(testClass, name);
resources = new String[] { resource };
}
DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment().name(testClass.getSimpleName() + "." + methodName);
for (String resource : resources) {
deploymentBuilder.addClasspathResource(resource);
}
if (deploymentAnnotation.tenantId() != null
&& deploymentAnnotation.tenantId().length() > 0) {
deploymentBuilder.tenantId(deploymentAnnotation.tenantId());
}
deploymentId = deploymentBuilder.deploy().getId();
}
return deploymentId;
}
统一根据注解部署流程