JUnit4 support for in-memory tests in order to use @BeforeClass

113 views
Skip to first unread message

Alfonso Mateos Alarcón

unread,
Sep 11, 2014, 7:14:27 AM9/11/14
to camunda-...@googlegroups.com
Hi guys,

I'm mocking my serviceTasks in order to test my process, but digging into Mock.register I saw that it's using a static Map.
So it seems that I could intialize and register my mocks just once, and not in every test method. If so, I would greatly improve the readability of my tests.

So I'm wondering if I could use a method that would be executed just once at the very beginning of my test, just as @BeforeClass does for JUnit4. 
Since ProcessEngineTestCase extends TestCase, it seems that I cannot use that annotation.

Do you have any hint to do so? I thought about the possibility of instantiating taskService, processEngine, etc. on my own, but ProcessEngineTestCase include more features than this...

Martin Schimak

unread,
Sep 11, 2014, 7:47:37 AM9/11/14
to camunda-...@googlegroups.com
Hi,

Instead of ProcessEngineTestCase with junit4 you can use ProcessEngineRule

  @Rule
  public ProcessEngineRule processEngineRule = new ProcessEngineRule();

The rule will make available everything you need pretty much like ProcessEngineTestCase.

However, I would also recommend to register the mocks in a @Before annotated method (or setUp() for JUnit 3), not @BeforeClass, so something like...

 @Mock
  Strategy myStrategy;

 @Before
  public void setUp() {
    MockitoAnnotations.initMocks( this );
    Mocks.register("myStrategy", myStrategy);
  }

… since the mocks always need to be registered in the same thread the test method uses - so this also works in case you use a multithreaded test runner. Even more important: you will want to set up mock behavior and verify expectations on your mocks (like called methods), but don't want your test methods to interfere with each other by using shared mock instances.

Many greetings,
Martin. 
--
You received this message because you are subscribed to the Google Groups "camunda BPM users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camunda-bpm-us...@googlegroups.com.
To post to this group, send email to camunda-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/camunda-bpm-users/f31c1170-d100-4bd7-a07e-91b9fef9cd09%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alfonso Mateos Alarcón

unread,
Sep 11, 2014, 8:04:45 AM9/11/14
to camunda-...@googlegroups.com
Thanks a lot, Martin! I didn't consider that issue about threads...
@Mock is from Mockito, isn't it? By the way, did you use @InjectMocks in order to inject mocks inside an instance too? 

Thanks in advance!

Martin Schimak

unread,
Sep 11, 2014, 8:10:54 AM9/11/14
to camunda-...@googlegroups.com
@Mock is Mockito, yes. I haven't yet used @InjectMocks when using Mockito with camunda. Many greetings, Martin.
PS: You also might want to check out https://github.com/camunda/camunda-bpm-assert when writing junit tests for camunda. ;-)

Alfonso Mateos Alarcón

unread,
Sep 11, 2014, 9:56:11 AM9/11/14
to camunda-...@googlegroups.com
Thanks a lot, Martin, understood ;-)

There's a little issue yet, and it's about deployment... Inside ProcessEngineTestCase I find the following line:

   deploymentId = TestHelper.annotationDeploymentSetUp(processEngine, getClass(), getName());

That is supposed to deal with the annotation @Deployment(resources = "diagrams/myProcessDiagram.bpmn") 

If you could give me any hint about how can I read that annotation from my JUnit4 test that would be really useful.

Thanks a lot in advance  :-)


Martin Schimak

unread,
Sep 11, 2014, 10:12:12 AM9/11/14
to camunda-...@googlegroups.com
Hi,

I am not sure what you mean exactly. You can use the @Deployment annotation with both ProcessEngineTestCase as well as ProcessEngineRule… and you can work with TestHelper and its methods in both cases.

@Test
@Deployment(resources = {"camunda-test-1.bpmn", "camunda-test-2.bpmn"})
public void testHappyPath() {
...
}


Martin.
--
You received this message because you are subscribed to the Google Groups "camunda BPM users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camunda-bpm-us...@googlegroups.com.
To post to this group, send email to camunda-...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages