# Junit for custom JavaDelegate

**URL:** <https://forum.flowable.org/t/junit-for-custom-javadelegate/10449>\
**Category:** Uncategorized\
**Created:** [April 25, 2023, 2:38pm UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449 "2023-04-25T14:38:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jothianand](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/jothianand/32/3157_2.png) [@jothianand](https://forum.flowable.org/u/jothianand)\
**Post date:** [April 25, 2023, 2:38pm UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449/1 "2023-04-25T14:38:57Z")

</div>

I have a custom JavaDelgate class and i would like to add junit test case for the execute method. Do we have any samples for the same ?

---

<div class="post-metadata">

**Author:** ![amporsim](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@amporsim](https://forum.flowable.org/u/amporsim)\
**Post date:** [April 28, 2023, 8:24am UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449/2 "2023-04-28T08:24:09Z")

</div>

Hi @jothianand

You could write a junit test which deploys a test process that uses your custom JavaDelegate. With that approach you have a proper integration test.

```auto
@Test
@Deployment(resources = "test-process.bpmn20.xml")
public void test() {
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
                .processDefinitionKey("testProcess")
                .start();
}

```

test-process.bpmn20.xml:

```auto
<process id="testProcess" name="testProcess" isExecutable="true">
    <startEvent id="start" />
    <endEvent id="end"/>
    <serviceTask id="serviceTask" flowable:delegateExpression="${yourJavaDelegate}"/>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="serviceTask"/>
    <sequenceFlow id="flow2" sourceRef="serviceTask" targetRef="end"/>
  </process>

```

Regards,  
Simon

---

<div class="post-metadata">

**Author:** ![jothianand](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/jothianand/32/3157_2.png) [@jothianand](https://forum.flowable.org/u/jothianand)\
**Post date:** [April 28, 2023, 1:03pm UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449/3 "2023-04-28T13:03:32Z")

</div>

Thanks Simon.

QQ on the test case you shared, what exactly do we assert here ? Could you please share the complete test case if you have.

Also i’m more interested on unit tests for each component. Like, if we have a bunch of listeners, delegates, etc. in our process, then how can we write unit test cases for each one of them.

Regards,  
Jothianand S

---

<div class="post-metadata">

**Author:** ![martin.grofcik](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/martin.grofcik/32/71_2.png) [@martin.grofcik](https://forum.flowable.org/u/martin.grofcik)\
**Post date:** [April 28, 2023, 1:43pm UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449/4 "2023-04-28T13:43:04Z")

</div>

try to use archetype to generate a project:

> **[Tooling · Flowable Open Source Documentation](https://www.flowable.com/open-source/docs/bpmn/ch19-tooling#maven-archetypes)**
>
> \## JMX

or have a look into flowable source, there are plenty of jUnit tests.

---

<div class="post-metadata">

**Author:** ![amporsim](https://avatars.discourse-cdn.com/v4/letter/a/ea666f/32.png) [@amporsim](https://forum.flowable.org/u/amporsim)\
**Post date:** [May 3, 2023, 6:17am UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449/5 "2023-05-03T06:17:46Z")

</div>

> [@jothianand](#):
>
> QQ on the test case you shared, what exactly do we assert here ? Could you please share the complete test case if you hav

For instance if your Java Delegate adds a variable to the process, you could test it afterwards:

```auto

public class YourJavaDelegate implements JavaDelegate {
    @Override
    public void execute(DelegateExecution execution) {
        execution.setVariable("foo", true);
    }
}

```

```auto
@Test
@Deployment(resources = "test-process.bpmn20.xml")
public void test() {
ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder()
                .processDefinitionKey("testProcess")
                .start();

assertThat((Boolean) runtimeService.getVariable(processId, "foo")).isTrue();
}

```

---

<div class="post-metadata">

**Author:** ![jothianand](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/jothianand/32/3157_2.png) [@jothianand](https://forum.flowable.org/u/jothianand)\
**Post date:** [May 6, 2023, 1:08pm UTC](https://forum.flowable.org/t/junit-for-custom-javadelegate/10449/6 "2023-05-06T13:08:16Z")

</div>

Thanks Martin and Simon, will try these out.
