10-20-2014 07:59 AM
I have following problem. I try to unit test activiti flow using Mock framework provided by activiti. I have the following activiti.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="jobExecutorActivate" value="false" />
<property name="expressionManager">
<bean class="org.activiti.engine.test.mock.MockExpressionManager" />
</property>
</bean>
</beans>
Then in the test I use ActivitiRule
to run the process:
@Rule
public ActivitiRule activitiRule = new ActivitiRule();
@Before
public void onInit() throws Exception {
Mocks.reset();
//Deploy flows ...
Mocks.register("timeService", new TimeServiceImpl(null));
}
private void execute() {
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("req", request);
activitiRule.getRuntimeService().startProcessInstanceByKey("ReturningFlow", variables);
}
Problem that I have is that all objects registered with Mock.register() are available for use inside ServiceTasks (inside the expression), but are not available inside groovy scripts. I get:
groovy.lang.MissingPropertyException: No such property: timeService for class: Script1
So the questions:
Am I doing something wrong? Is this an intended behavior of Activiti? What can I do in order to test scripts inside the flow using Mock objects?
Thanks in advance.
10-20-2014 11:37 AM
So after some time experimenting, I kind of hacked a solution.
We need an MockResolver for scripting objects:
package com.deenero.activiti;
import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.impl.scripting.Resolver;
import org.activiti.engine.impl.scripting.ResolverFactory;
import org.activiti.engine.test.mock.Mocks;
public class MockResolverFactory implements ResolverFactory{
@Override
public Resolver createResolver(VariableScope variableScope) {
return new Resolver() {
@Override
public Object get(Object key) {
return Mocks.get(key);
}
@Override
public boolean containsKey(Object key) {
return Mocks.get(key) != null;
}
};
}
}
And then activiti configuration needs to be changed to use it:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">
<property name="jobExecutorActivate" value="false" />
<property name="expressionManager">
<bean class="org.activiti.engine.test.mock.MockExpressionManager" />
</property>
<property name="resolverFactories">
<list>
<bean class="org.activiti.engine.impl.scripting.VariableScopeResolverFactory" />
<bean class="org.activiti.engine.impl.scripting.BeansResolverFactory" />
<bean class="com.deenero.activiti.MockResolverFactory" />
</list>
</property>
</bean>
</beans>
So this will allow to use all beans registered through Mock.register() inside Activiti scripts, and therefore we are able to unit test them.
10-21-2014 03:15 AM
10-21-2014 03:26 AM
10-22-2014 03:18 AM
10-22-2014 07:14 AM
10-22-2014 07:19 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.