JUnit Testing with Autowiring

I am trying to write a bpmn delegate test but its failing with an error that it cannot instantiate the delegate class.
The problem is that I have a parameterized constructor for my delegate for dependency injection. If I include a default constructor the test case works but it fails to instantiate the delegate with a parameterized constructor.

private ObjectMapper objectMapper;

public MyDelegate( ObjectMapper objectMapper) {

this .objectMapper = objectMapper;

}

@Override

public void execute( DelegateExecution delegateExecution) {

try {}

so if fails here:
ProcessInstance instance = processEngineRule.getProcessEngine().getRuntimeService().startProcessInstanceByKey(“MyProcess”,processVariables);

Hi,

I assume you are trying to run a Unit test without Spring context. In this case, you have to register the delegate so that the workflow engine can find it.

You have to do the following

import org.camunda.bpm.engine.test.mock.Mocks;

@Before
public void setUp() {
    Mocks.register("nameOfYourDelegateInProcess", new MyDelegate(new ObjectMapper()));
}

Alain

Already tried that it doesn’t work

Caused by: org.camunda.bpm.engine.ProcessEngineException: couldn’t instantiate class com.sample.test.demo.MyDelegate at org.camunda.bpm.engine.impl.DefaultArtifactFactory.getArtifact(DefaultArtifactFactory.java:38)
at org.camunda.bpm.engine.impl.util.ClassDelegateUtil.instantiateDelegate(ClassDelegateUtil.java:48)
… 122 common frames omitted

Tried ContextConfiguration that didn’t work either. I think its the test process engine which only looks for a default constructor if there is a parameterized constructor in a delegate the test process engine cannot instantiate that delegate.

Following up with the post if anyone has a solution for it.