Hello dear camunders,
- What do I have.
I have simple BPM application with the single flow wrapped into spring boot.
Themy.bpmn
has three nodes:
a) Start node
b) Service task with the implementation of type “delegate expression” with the value${myServiceJava}
c) End node
The classMyServiceJava
is:
package my.pckg;
import *imports-imports-imports...*
@Component
public class MyServiceJava implements JavaDelegate {
@Autowired
private SomeOtherSpringComponent otherComponent;
public void execute(DelegateExecution execution) {
// The code is here; it uses otherComponent
return;
}
}
The application works fine and does what I want.
- The problem
I must develop unit test for this application. So I wrote this:
package my.pckg;
[...skiped imports...]
@ExtendWith({ProcessEngineExtension.class, SpringExtension.class, MockitoExtension.class})
@SpringJUnitConfig(locations="/camunda.cfg.xml")
@TestPropertySource("classpath:application-test.properties")
public class SimpleTest {
public ProcessEngine processEngine;
@Test
@Deployment(resources = {"my.bpmn"})
public void test01() {
Map<String, Object> startVars = new HashMap<>();
// Populate startVars here
RuntimeService rts = processEngine.getRuntimeService();
ProcessInstance procInst = rts.startProcessInstanceByKey( //
"MyProc", //
"business-key-123", //
startVars);
// More code here...
}
}
When I run the test, I get NullPointerException
- the otherComponent
in my Java delegate is NOT autowired and is null
and NPE
is thrown when execute()
tries to use otherComponent
. It is 100% true - I see this in the debugger. The service task node gets control, the Java delegate class in successfully instantiated by spring, its method execute()
is called, but object’s member variable otherComponent
is null
.
!!! At the same time, the application itself works fine, spring succesfully resolves and injects all dependencies.
The questions:
- what do I do wrong in my unit test?
- how do you do JUnit tests with heavy use of spring autowire in Java delegates, event listeners etc?
Thanks,
Yuriy