cancel
Showing results for 
Search instead for 
Did you mean: 

[Sticky] How to write a unit test

jbarrez
Star Contributor
Star Contributor
When encountering a bug in Activiti, the easiest way to reach the Activiti team and see it fixed is to create an issue in the Activiti Jira (http://jira.codehaus.org/browse/ACT).

Of course, a good description on how to reproduce the problem is crucial. But to make it really interesting for us (and to speed up things), the issue is accompanied by a unit test demonstrating the problem.

To simplify this process, we've created a 'unit test template' project which you can use: https://github.com/Activiti/activiti-unit-test-template
Add your failing unit test, zip up the project and attach it to the issue.

More about this template, read http://www.jorambarrez.be/blog/2012/09/24/how-to-write-a-unit-test/
27 REPLIES 27

jbarrez
Star Contributor
Star Contributor
The unit tes here is not a SpringProcessEngineConfiguration, hence why spring beans won't work.

brett1
Champ in-the-making
Champ in-the-making
Of course, a good description on how to reproduce the problem is crucial. But to make it really interesting for us .thanks <a href="http://www.redriverfurnace.com">air duct cleaning</a>

martygross
Champ in-the-making
Champ in-the-making
Thanks jbarrez!

juegos de casino

ozkan
Champ in-the-making
Champ in-the-making
Thanks jbarrez.

sopheachhun
Champ in-the-making
Champ in-the-making
Dear jbarrez,

I download your template from here:https://github.com/Activiti/activiti-unit-test-template/blob/master/src/test/java/org/activiti/MyUni...

I just modify the location of the process file.

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.test.ActivitiRule;
import org.activiti.engine.test.Deployment;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.*;

public class UnitTestActiviti{

@Rule
public ActivitiRule activitiRule = new ActivitiRule();

@Test
@Deployment(resources = {"E:/Dropbox/workspaceEclipse/eclipseJuno/customer/addition.bpmn"})
public void test() {
ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("Addition process");
assertNotNull(processInstance);

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

}

Here is the message error I get: java.lang.Exception: Field activitiRule must implement MethodRule
at org.junit.runners.BlockJUnit4ClassRunner.validateRuleField(BlockJUnit4ClassRunner.java:181)
at org.junit.runners.BlockJUnit4ClassRunner.validateFields(BlockJUnit4ClassRunner.java:176)
at org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:116)
at org.junit.runners.ParentRunner.validate(ParentRunner.java:269)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:66)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:59)
at org.junit.internal.builders.JUnit4Builder.runnerForClass(JUnit4Builder.java:13)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Please help me, i am new to unitTest,

thanks inadvance,
sophea

jbarrez
Star Contributor
Star Contributor
Did you import this as a Maven project? Do you have anything else on your classpath unit test/test related?

I've tried the template and it runs out of the box here. Does the default example process work for you?

markv
Champ in-the-making
Champ in-the-making
I'm trying to use this template in activiti 5.17.0 to examine odd (but consistently reproduceable) behavior in TimerStartEvents. My first issue in doing this is to get the job executor started. The following code fails at the assertion <code>assertTrue(active)</code>, even though my understanding is that the call to <code>setJobExecutorActivate(true)</code> should be starting the jobExecutor for this ProcessEngine instance. What am I doing wrong?
<java>
@Test
public void test() {
  ProcessEngine processEngine = activitiRule.getProcessEngine();
  processEngine.getProcessEngineConfiguration().setJobExecutorActivate(true);
  RepositoryService repositoryService = processEngine.getRepositoryService();
  repositoryService.createDeployment()
  .addClasspathResource("org/activiti/test/ServiceTaskTimerTest.bpmn")
  .deploy();
  ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("serviceTaskTimerTest");
  assertNotNull(processInstance);
  boolean active = processEngine.getProcessEngineConfiguration().getJobExecutor().isActive();
  assertTrue(active);
// Other stuff…
</java>

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Mark,

have a look on org.activiti.engine.impl.test.JobTestHelper#waitForJobExecutorOnCondition(org.activiti.engine.ProcessEngineConfiguration, long, long, java.util.concurrent.Callable<java.lang.Boolean>)in the activiti source
You can see there how to start jobExecutor.

Regards
Martin

markv
Champ in-the-making
Champ in-the-making
Thank you, Martin, this solved the problem. For those who may be interested, the relevant code to kick off the JobExecutor (class org.activiti.engine.impl.jobexecutor.JobExecutor) for a unit test looks like this:
<java>
//
// Use one of the many ways to construct an org.activiti.engine.ProcessEngine object.
//
JobExecutor jobExecutor = processEngine.getProcessEngineConfiguration().getJobExecutor();
jobExecutor.start();
</java>

yongqiang
Champ in-the-making
Champ in-the-making
Hi jbarrez,

I received issue "Caused by: org.h2.jdbc.JdbcSQLException: Table "ACT_HI_PROCINST" already exists; SQL statement:" when I use the given junit template. May I know how to clean h2 before test?

In addition, I would like to know how to let activitirule to load configuration spring.


Regards,
Yong