How to write a unit test for user assignee

Hi
How can we write a unit test for the userTask assignee?

I created a form in start event and i want which number the user was put there, the following userTask assignee must that number.

I don’t know how can test this in unitTest

assertEquals("user", taskService.createTaskQuery().singleResult().getAssignee());

 @Autowired
 private RuntimeService runtimeService;

@Autowired
 private TaskService taskService;

@Test
@Deployment
public void assigneeTest() {
    runtimeService.startProcessInstanceByKey("simpleProcess");
    Task task = taskService.createTaskQuery().singleResult();
    assertEquals("user", task.getAssignee());
 }
1 Like

Hi @rezza72,

from your other post it seems like BPM Assert is on your project’s path, so I’d recommend doing something like:

package com.example.test;

import static org.camunda.bpm.engine.test.assertions.ProcessEngineTests.*;

import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.test.Deployment;
import org.camunda.bpm.engine.test.ProcessEngineRule;
import org.junit.Rule;
import org.junit.Test;

public class AssigneeTest {

  @Rule
  public ProcessEngineRule engineRule = new ProcessEngineRule(true);

  @Test
  @Deployment
  public void shouldAssignUser() {
    ProcessInstance processInstance = runtimeService().startProcessInstanceByKey(
        "process-definition-key", withVariables("user_id", "83282929"));
    assertThat(processInstance).task().isAssignedTo("83282929");
  }
}

No need to auto-wire the services yourself, no need to excessively use the low-level engine API in your tests.

Just make sure the model under test can be found at the location defined in the @Deployment annotation as documented.

Hope that helps.

Best,
Tobias

1 Like