cancel
Showing results for 
Search instead for 
Did you mean: 

Activiti is not using object registered with Mocks.register() inside groovy scripts

emilgenov
Champ in-the-making
Champ in-the-making
Activiti is not using object registered with Mocks.register() inside groovy scripts

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.

6 REPLIES 6

emilgenov
Champ in-the-making
Champ in-the-making

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.

jbarrez
Star Contributor
Star Contributor
Yes, that is indeed the way to solve it. Otherwise the scripting engine has no clue about the objects.

This would be a very nice addition to the code. Would you be willing to create a pull request containing this + a little bit of doc?

emilgenov
Champ in-the-making
Champ in-the-making
Sure. I'll try do it in the weekend.

emilgenov
Champ in-the-making
Champ in-the-making
Hi Joram,

I've made the pull request: https://github.com/Activiti/Activiti/pull/412

Best Regards
Emil

jbarrez
Star Contributor
Star Contributor
Many thanks - will look at it soon (swamped with pull requests currently so could take a while - lots of ppl on holiday)

emilgenov
Champ in-the-making
Champ in-the-making
Thank you too Smiley Happy