cancel
Showing results for 
Search instead for 
Did you mean: 

Injecting class in (JAVA) serviceTask manually for testing

sruiz
Champ in-the-making
Champ in-the-making
Is it possible to inject manually in the service task the class for testing purposes?

As I read, the JavaDelegate object is injected in the process definition first time the process is instanced.

The problem is that I would like to inject a RuntimeService component (the runtimeService from the ActivitiRule test) before.

I've found a topic relatedSmiley Sadhttp://forums.activiti.org/en/viewtopic.php?f=3&t=249&hilit=runtimeservice+servicetask).

I tried in the test to use:
ProcessEngineImpl engine = (ProcessEngineImpl) ProcessEngines.getDefaultProcessEngine();
for accesing to the current runtimeService of the test but it creates a new engine. I think that it is due to the fact that I'm using an in-memory database so it creates a new database in other memory location.

So I understand that the only way is using Spring Injection (http://forums.activiti.org/en/viewtopic.php?f=3&t=249&hilit=runtimeservice+servicetask).

Is there other way of testing a process without the use of Spring ?
The idea in the test should be:
1. Deploy process.
2. Inject all java beans in the several JAVA service tasks of the process. Initialize within the test these beans.
3. Run the test.
3 REPLIES 3

frederikherema1
Star Contributor
Star Contributor
Hi,

If your goal is to use the RuntimeService from within your JavaDelegate implementation, then you should rethink this Smiley Wink

The API is created to be used outside of the process-context. When you will try to use a service (eg. runtimeService) in the JavaDelegate, transactions an command-context will be messed up.

sruiz
Champ in-the-making
Champ in-the-making
I agree that a rule of thumb should be
Don't change process flow with code, use only process variables
.

I didn't want to change flow using runtimeService. I only wanted to access to parent process variables within a subprocess  using ProcessInstanceQueries.

Execution class should access to parent variables or a best solution: access to parent execution. Currently I can not.

I only try to find a temporary solution to:
http://jira.codehaus.org/browse/ACT-313
http://jira.codehaus.org/browse/ACT-354

:cry: Excuse me, maybe the question shall be in other topic:
Access to parent variables within a sub-process with code
.
If you want I open a new topic with this issue.

frederikherema1
Star Contributor
Star Contributor
Ah, now I see. A workaround could be to use the activiti-sessions instead of API to get execution-variables from parent, eg:

CommandContext
      .getCurrentSession(RuntimeSession.class)
      .findVariableInstancesByExecutionId(parentExecutionId);

or

CommandContext
      .getCurrentSession(RuntimeSession.class)
      .findExecutionById(parentExecutionId);


Since this is not part of the public API, we can't guarantee that the method-signature (or method itself) will remain the same over time. Also note that this code will only work when invoked from within a process-context (eg. taskListener or JavaDelegation).