Well, it’s difficult to unit test this kind of functionality, because it’s basically Camunda’s code - and they have an extensive unit test suite to make sure it’s working.
I would recommended that you first focus should be on unit testing your own business code components. Components that perform some kind of logic / computations that are important to your domain.
Second, you could look into testing your process to verify that your process flow / logic is correct. Such a test would most likely also test logic such as the above (assuming it’s called as part of a process, off course).
Have a look at this to read more about how to test a hole (or part of) process:
class CamundaMethodsTest {
private CamundaMethods camundaMethods;
private ProcessEngine usedProcessEngine = ProcessEngineConfiguration
.createStandaloneInMemProcessEngineConfiguration()
.setJdbcUrl("jdbc:h2:mem:camunda;DB_CLOSE_DELAY=1000")
.buildProcessEngine();
@RegisterExtension
ProcessEngineExtension extension = ProcessEngineExtension
.builder()
.useProcessEngine(usedProcessEngine)
.build();
@BeforeEach
public void setup() {
camundaMethods = new CamundaMethods(usedProcessEngine);
}
@Test
@Deployment(resources = "TestExtTask.bpmn")
void whenExtTaskIsActiveThanGetTopicNamesReturnExpectedList() {
// Given we create a new process instance
ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("TestExtTask");
// Then it should be active
assertThat(processInstance).isActive();
// And it should be the only instance
assertThat(processInstanceQuery().count()).isEqualTo(1);
List<String> actualTopicNames = camundaMethods.getTopicNames();
org.junit.jupiter.api.Assertions.assertEquals("test-topic", actualTopicNames.get(0));
}
@Test
@Deployment(resources = "TestExtTask.bpmn")
void whenExtTaskIsActiveThanGetLocketExtTaskReturnExpectedCountTask() {
ProcessInstance processInstance = runtimeService().startProcessInstanceByKey("TestExtTask");
List<LockedExternalTask> lockedExternalTasks = camundaMethods.getLockedExternalTasks("test-topic", "workerId");
org.junit.jupiter.api.Assertions.assertEquals(1, lockedExternalTasks.size());
}
}
if i run tests separately it works fine.
but if i run tests all together it fails with the error
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Таблица “ACT_HI_PROCINST” уже существует
Table “ACT_HI_PROCINST” already exists; SQL statement:
it seems like h2 database initialization runs before each test.
how to prevent this behavior?