Running an activity in the bpmn for testing

Hi,
We have several flow which which we used to test in two phases.
phase one - each activity individually
phase two - all the flow in functional tests.
for phase one we used to have a bpmn with start_event → Activity → end_event
then we copy it to the complete flow.

This approach was proved to be very problematic and now we have allot of code that needs refactoring.
We don’t use Spring boot and this is the best solution I found for starting the flow at a certain activity:

@Rule
public ProcessEngineRule processEngineRule = 
          TestCoverageProcessEngineRuleBuilder.create().build();

@Test
@Deployment(resources = "flow.bpmn")
private ProcessInstance runCamundaActivity() {
    RuntimeService runtimeService = processEngineRule.getRuntimeService();
    return runtimeService.createProcessInstanceByKey("flowId")
            .startBeforeActivity("activityId")
            .setVariables(Utils.initializeVariables())
            .executeWithVariablesInReturn();
}

This was somewhat transparent and easy way to refactor our tests. However when the activity is in the beginning of the flow there’s no way to mark the activity to stop. So the unit test actually runs until the end of the flow.
for the process builder there’s only startBeforeActivity and startAfterActivity but no stopAfterActivity.
what’s the best way to go about it?
Is there a way to tell the flow to stop after the activity ends?

Best Regards,
Itamar

Testing woks by starting from a certain point and continuing until the transaction is complete.
These means that if there are no transaction boundaries between the point where you’re starting the process and the end it will just complete the whole process.

One option would be to add transaction boundaries to your process by ticking async before/after on tasks.

Thank you for the reply
Unfortunately this solution didn’t work and resulted in exceptions regarding the persistence procedure.
Moreover marking the activities as async after will cause other problems of performance and might effect the flow itself.
I’ve tried to work with ProcessInstanceModification However couldn’t find a way to execute and then get the execution variables to assess them.
Another option was to add to the listener end script a check to a global variable and exit the flow. Is there a command I can use on the execution to do that without triggering an exception?

Thanks for your assistance