How to write a unit test when creating an Activiti Jira issue

Often, people in the Activiti forum report bugs, or they create issues in the Activiti Jira. That’s simply awesome, and that is one of the reasons why open-source software will always be one step ahead of any proprietary solution.

Of course, we’ve only got some much time in a day so there are a few things that can help us when creating such bug reports. First of all, of course a good description on how to reproduce the problem is key. But it helps us the most, when you attach a unit test to the issue which shows us the problem straight away. In this post, I’ll show you how you can easily create such unit test.

If you want to skip all the explanations, here is the link to the project template which contains all of the stuff described below: https://github.com/Activiti/activiti-unit-test-template

Step 1: create a new maven project (File -> new -> other -> Maven project for Eclipse using m2eclipse). Maven certainly is helpful, because there can’t be discussion about which dependencies are used. Add the dependencies for Activiti, the database driver (h2 if it’s a non-db specific issue) and JUnit to your pom.xml. Also note these are the versions of the jars at the time of writing.


<dependencies>
  <dependency>
    <groupId>org.activiti</groupId>
    <artifactId>activiti-engine</artifactId>
    <version>5.10</version>
  </dependency>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
  </dependency>
  <dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.3.168</version>
    <scope>test</scope>
  </dependency>
</dependencies>

<repositories>
  <repository>
    <id>Alfresco Maven Repository</id>
    <url>https://maven.alfresco.com/nexus/content/groups/public/</url>
  </repository>
</repositories>

Step 2: Add your Activiti configuration file (activiti.cfg.xml) to src/test/resources. The most basic Activiti config, which uses an in-memory H2 database looks as follows:


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneInMemProcessEngineConfiguration">

    <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />

    <property name="jdbcDriver" value="org.h2.Driver" />

    <property name="jdbcUsername" value="sa" />

    <property name="jdbcPassword" value="" />

  </bean>

</beans>

Step 3: Add your bpmn20.xml file (if needed) to src/test/resouces. For example:

Step 4: Create a Junit test demonstrating the problem. For example:


import static org.junit.Assert.*;

public class MyUnitTest {

  @Rule
  public ActivitiRule activitiRule = new ActivitiRule();

  @Test
  @Deployment(resources = {"org/activiti/test/my-process.bpmn20.xml"})
  public void test() {
    ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process");
    assertNotNull(processInstance);

    Task task = activitiRule.getTaskService().createTaskQuery().singleResult();
    assertEquals("Activiti is awesome!", task.getName());
  }

}

All of the above can be downloaded as simple-to-use template from GitHub: https://github.com/Activiti/activiti-unit-test-template

Looking forward to all of your new Jira issues, accompanied by magnificent unit tests!

21 Comments

  1. Faeze January 13, 2013

    Hi
    thanks for your tutorial.
    I’m newbie in Activiti and as I understand first step is creating Activiti project in eclipse that it contains activiti.cfg.xml file in src/test/resources but its content is less than yours,Do steps change by altering Activiti version? And what should I do if I only want to use Activiti project without Maven?
    Thanks
    Faeze

  2. Joram Barrez January 14, 2013

    @Faeze: Yes, you can have less config. The less you have, the more defaults will be used.

    You can use Activiti without Maven, but then you’ll need to gather all the jars yourself from the maven repository. We do advise to use Maven (or Ivy).

  3. Faeze January 14, 2013

    thanks Joram
    I applied direction as you described here by using your code from GitHub, but Junit shows 1 error which is:
    org.activiti.MyUnitTest [Runner: JUnit 4] (0.016 s)
    initializationError (0.016 s)
    org.activiti.MyUnitTest [Runner: JUnit 4] (0.016 s)
    initializationError (0.016 s)
    Also Failure Trace shows:
    java.lang.NoClassDefFoundError: LActivitiRule;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Unknown Source)
    at java.lang.Class.getDeclaredFields(Unknown Source)
    java.lang.NoClassDefFoundError: LActivitiRule;
    at java.lang.Class.getDeclaredFields0(Native Method)
    at java.lang.Class.privateGetDeclaredFields(Unknown Source)
    at java.lang.Class.getDeclaredFields(Unknown Source)
    Now How can I solve it?

  4. Joram Barrez January 14, 2013

    The ActivitiRule is part of the activiti-engine jar. Are you sure it is on your classpath?

  5. Faeze January 15, 2013

    I have it in my Maven dependency in org.activiti.engine.test package as ActivitiRule.class. Is it enough? Does it need to copy it somewhere else?

  6. Joram January 17, 2013

    Faeze: no, that dependency is the only thing. Looking at the exception, there is something wrong with your project setup. Any chance you could put it online on GitHub?

  7. Faeze January 19, 2013

    I pushed my project in https://github.com/Ghorbannezhad/try_git/tree/Activiti and will be so appreciate if you look at it.
    thanks a lot

  8. Joram Barrez January 21, 2013

    @Faeze: Okay, it took me a while to figure out you added the code under the ‘activiti’ branch on that repo :p All i was seeing was octocats text files first :p

    The problem is pretty simple … you added the test class to src/test/resouces instead of using src/test/java. It is never going to work, as src/test/resources is a not set up for compiling java classes. I would advise you read up on Maven first. Or if you’d cloned my project of this blogpost as-is, you’d see the Test classes are part of src/test/java ….

  9. Faeze January 21, 2013

    oooh yes, I make a big mistake:)
    but bad news is I correct its path but still error exists:(
    I put 2 pictures of my maven project in github: https://github.com/Ghorbannezhad/try_git/tree/EclipseJpeg(it's in new branch called EclipseJpeg)

  10. Faeze January 21, 2013
  11. joram.barrez@gmail.com January 22, 2013

    That looks ok. But I rather see it in code. Could you update your code on github?

  12. Faeze January 22, 2013

    Of course Yes, here it is:
    https://github.com/Ghorbannezhad/try_git/tree/NewProject
    thanks alot

  13. Joram Barrez January 23, 2013

    @Faeze: I cloned the project and imported it into Eclipse. It just runs green here.

  14. Faeze January 23, 2013

    Could you please tell me what do you do exactly after cloning the project?Maybe one step has been missed in my procedure.

  15. Faeze January 24, 2013

    Sure:

    git clone https://github.com/Ghorbannezhad/try_git.git
    git checkout NewProject

    Open up Eclipse, File -> import -> Import Maven project

  16. Arch February 22, 2013

    Hi. Thanks for the tutorial.

    I’ve run into a snag with Maven and the POM file. I’m using a MacBook Pro running OS X 10.7.5 and Maven 3.0.3. My steps were:
    – downloaded the zip of the Maven project template from https://github.com/Activiti/activiti-unit-test-template
    – unzipped template
    – Inside Eclipse Juno Release 1, I right clicked Project Explorer and Import -> Maven Projects …
    – Project imported but I received POM execution ArtifactTranserException errors for org.hamcrest:hamcrest-core:jar:1.1 and junit:junit:jar:4.10.

    Would you have any thoughts about my mistake here?

    Any guidance would be greatly appreciated. Thanks.

  17. Arch February 22, 2013

    I can post the full POM error messages because when I post it the system thinks I’m spamming. So here is what one of the messages reads.

    ArtifactTransferException: Failure to transfer org.hamcrest:hamcrest-core:jar:1.1 from http://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced. Original error: Could not transfer artifact org.hamcrest:hamcrest-core:jar:1.1 from/to central (http://repo.maven.apache.org/maven2): Invalid thread access

    Thanks.

  18. Arch February 22, 2013

    I performed a right-click Project -> Maven -> Update Projects … several times and switched to version 4.11 of Junit and this corrected my issue. The unit test now runs correctly. My apologies for the newbie errors.
    Thanks.

  19. Joram Barrez February 22, 2013

    @Arch: no worries! Glad you figured it out on your own. But it does make me wonder: was it related to your system or does the template simply doesn’t work at all?

    Ie. why was the upgrade to 4.11 of Junit necessary?

  20. Eddu January 30, 2014

    File with extension *.bpmn20.xml is not generated. I am using activiti 5.14 and activiti designer 4.14. When i try to use extension .bpmn my junit test fails.

  21. Joram Barrez January 31, 2014

    @Eddu: so you mean that a .bpmn file in the @Deployment doesn’t work?
    If so, that is a bug for sure and needs fixing!

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>