Problem testing spring boot web layer

Hello.

I have a simple process:

SimpleTestProcess.bpmn (3.0 KB)

Running a test like:

package de.sternico.education.bpmn.process;

import static org.assertj.core.api.Assertions.in;
import static org.camunda.bpm.engine.test.assertions.bpmn.BpmnAwareTests.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.camunda.bpm.engine.test.mock.Mocks;
import org.camunda.bpm.extension.process_test_coverage.junit.rules.TestCoverageProcessEngineRuleBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit4.SpringRunner;

import de.sternico.education.bpmn.definition.BpmnDefinitions;
import de.sternico.education.bpmn.delegate.simple.CreateTestDtosDelegate;
import de.sternico.education.bpmn.repository.simple.TestEntityRepository;
import de.sternico.education.bpmn.service.simple.SimpleTestService;
import de.sternico.education.bpmn.util.ProcessTest;

@RunWith(SpringRunner.class)
@DataJpaTest
@Deployment(resources = "simple/SimpleTestProcess.bpmn")
public class SimpleProcessTest extends ProcessTest
{
    @Rule
    @ClassRule
    public static ProcessEngineRule processEngine = TestCoverageProcessEngineRuleBuilder.create().build();
    
    @Autowired
    private SimpleTestService simpleTestService;
    
    @Autowired
    private TestEntityRepository testEntityRepository;

    @Before
    public void setup()
    {
        Mocks.register(CreateTestDtosDelegate.class.getSimpleName(), new CreateTestDtosDelegate(simpleTestService));
    }

    @Test
    public void testSimpleProcess()
    {
        ProcessInstance instance = processEngine.getRuntimeService()
                .startProcessInstanceByKey(BpmnDefinitions.ProcessKeys.Simple.SIMPLE_TEST_PROCESS);
        
        assertThat(instance).isWaitingAt("TASK_TEST_1");
        
        TaskService taskService = processEngine.getTaskService();
        
        Task taskTest1 = taskService
                .createTaskQuery()
                .processInstanceId(instance.getId())
                .taskDefinitionKey("TASK_TEST_1")
                .singleResult();
        
        taskService.complete(taskTest1.getId());
        
        assertThat(instance).hasPassed("TASK_CTD");
        
        assertEquals(3, testEntityRepository.findAll().size());
    }

    @After
    public void teardown()
    {
        Mocks.reset();
    }
}

works, but invoking the same code (starting process instance, finish the human task which leads to invoking the service task) from a web layer test in spring in @RestController gives me:

Cannot resolve identifier 'CreateTestDtosDelegate'.

The delegate is:

package de.sternico.education.bpmn.delegate.simple;

import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import de.sternico.education.bpmn.service.simple.SimpleTestService;

@Component("CreateTestDtosDelegate")
public class CreateTestDtosDelegate implements JavaDelegate
{
    @Autowired
    private SimpleTestService simpleTestService;
    
    public CreateTestDtosDelegate(SimpleTestService aSimpleTestService)
    {
        super();
        this.simpleTestService = aSimpleTestService;
    }
    
    public CreateTestDtosDelegate()
    {
        this(null);
    }

    @Override
    public void execute(DelegateExecution execution) throws Exception
    {
        simpleTestService.createTestEntities("ganz", "schoen", "lustig");
    }
}

Please note that in the green (not web layer) test, i use constructor injection for the ‘SimpleTestService’ (you told me so in another forum post, and it works fine), which i do not do in the web layer test because i assume that there the spring container does the autowiring for me.

Any suggestions?

P.S.: Running the process from camunda spring boot cockpit show the same behaviour (delegate expression can not be resolved).

P.P.S.: Like suggested in

Invoking a Spring Bean from a BPMN 2.0 Service Task | docs.camunda.org,

i added a configuration bean like

package de.sternico.education.bpmn.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import de.sternico.education.bpmn.delegate.simple.CreateTestDtosDelegate;

@Configuration
public class SimpleTestContext
{
    @Bean
    public CreateTestDtosDelegate createTestDtosDelegate()
    {
        return new CreateTestDtosDelegate();
    }
}

but the beehaviour is still the same.

P.P.P.S.: Is it ok to use

@SpringBootApplication
@ComponentScan(basePackageClasses = {TrainDepartureService.class, FlatRentalService.class, SimpleTestService.class, CreateTestDtosDelegate.class})
public class BpmnTestApplication
{

or is it mandatory to use @ProcessAllpication to start up camunda spring boot?

P.P.P.P.S.: I think i exactly followed the instructions in

1.) Process model → Service Task → Delegate expression → ${createTestDtosDelegate}
2.) Delegate

@Component(value = "createTestDtosDelegate")
public class CreateTestDtosDelegate implements JavaDelegate
{

No clue why the delegate can not be found…

P.P.P.P.S.P.: I have made a new project setup where i refer to Test Your Processes With JUnit 5 | Camunda, and there, also, the delegate cannot be found. As in the old project, the web layer test also runs under junit5, i think in the way i mock my delegate in the case must be wrong:

    @Before
    public void setup()
    {
        Mocks.register(CreateTestDtosDelegate.class.getSimpleName(), new CreateTestDtosDelegate());
    }

process:

<bpmn:serviceTask id="TASK_CTD" name="Create test dtos" camunda:delegateExpression="#{CreateTestDtosDelegate}">

Any suggestion for that?

Thank you.

Hello, i have done a new setup. So i do not have the problem anymore…