Task attachment unit test: no content

Hi, I’m working on a Unit test using FlowableRule. I have a TaskListener configured as follows:

    <userTask id="myTask" name="My Task" >
                    class="com...MyTask" />


    TaskService taskService = FlowableRule.getTaskService();

    String taskId = taskService.createTaskQuery().singleResult().getId();

    List<Attachment> taskAttachments = FlowableRule.getTaskService().getTaskAttachments(taskId);
    AttachmentEntity attachment = (AttachmentEntity) taskAttachments.get(0);

id is not null
content is null

How can I get the content populated in FlowableRule tests?


This works:


What? Bug or feature? Why can’t I get the attachment content from the attachment itself?

That is by design: the Attachment interface does not have a getContent() method. That’s why you have to cast to the AttachmentEntity to get it, which is not part of the API.
The reason for this is that getting the content needs to be done within a transaction. In this snippet:

AttachmentEntity attachment = (AttachmentEntity) taskAttachments.get(0);

there is no transaction as this is a simple Entity class that only holds data. However, the getAttachmentContent does wrap it in a transaction as it passed through the service.