JUnit Testing & Mock Service Task

#1

Has anyone mocked service task successfully using MockExpressionManager or anything in Spring Boot (Java)?

#2

Hi Bihan,

It really depends. One way of mocking could be to replace serviceTaskBehavior during process parsing.
https://flowable.org/docs/userguide/index.html#advanced_parseHandlers

Regards
Martin

#3

Hey @Bihan,

If you are using Spring Boot then I presume that you are using either a delegateExpression with a Spring bean or you are using an expression where you use your bean. This means that you could mock those beans by using @MockBean from Spring Boot. Have you tried using that?

Cheers,
Filip

#4

Yes, i am using delegateExpression. If possible can you give a simple sample on how to do it with @MockBeans for flowable.

I have tried with MockExpressionManager(), but this did not worked. My test runs the actual service task instead of the mock. I have discussed about it here Testing processes with junit in spring

#5

Hey,

Have you tried the standard way of working with @MockBean? You inject the bean and annotate it with @MockBean. Something like (test annotations are missing):

public class MyTest {

    @MockBean 
    @Autowired
    private MyDelegate myDelegate;

    @Test
    public void myTest() {
        // Do your thing
    } 
}

Cheers,
Filip

#6

Yes, I know this. Actually what i am asking is how does the process engine know that it should use the mocked service task not the actual service task . In similar testing with MockExpressionManager() provided by flowable itself, the process engine was unable to use mocked service task.

#7

If @MockBean is used then the Spring Application context knows that the bean is a mock. The Process Engine uses the ApplicationContextElResolver to get the bean.

1 Like
#8

Solved the problem. Thanks