AnsweredAssumed Answered

Unit test does not load test data

Question asked by kaihuener on Dec 14, 2013
Latest reply on Dec 19, 2013 by kaihuener
Hey all,

I want to write a simple unit test in a standard maven directory structure. An activiti.cfg.xml file is located in src/main/resources with all relevant configuration (database, ldap, mail, etc). For the unit test, I want to replace several configuration, e.g. H2 in-memory as database.

I also want to "bypass" LDAP authentification by (temporarily) adding a test user to the in-memory database. For that, I want to use the approach which the activiti team uses to add demo data, i.e. a spring bean that adds users to the (running) process engine by the identity service.

To achieve this, I added a simple activiti.cfg.xml to src/test/resources:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xsi:schemaLocation="">
   <bean id="testDataGenerator" class="cdq.cdl.processes.TestDataGenerator"
      <property name="processEngine" ref="processEngine" />
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
   <bean id="transactionManager"
      <property name="dataSource" ref="dataSource" />

   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
      <property name="dataSource" ref="dataSource" />
      <property name="transactionManager" ref="transactionManager" />
      <property name="databaseSchemaUpdate" value="true" />

   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"
      <property name="processEngineConfiguration" ref="processEngineConfiguration" />

The only "special" thing is the testDataGenerator bean:
import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.identity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TestDataGenerator {

   protected static final Logger LOGGER = LoggerFactory.getLogger(TestDataGenerator.class);

   protected transient ProcessEngine processEngine;
   protected transient IdentityService identityService;

   public void init() {
      this.identityService = processEngine.getIdentityService();"Initializing test users");

   public void setProcessEngine(ProcessEngine processEngine) {
      this.processEngine = processEngine;

   protected void initDemoUsers() {
      createUser("foo", "bar");

   protected void createUser(String userId, String password) {

      if (identityService.createUserQuery().userId(userId).count() == 0) {

         User user = identityService.newUser(userId);

Finally, I have a simple test case:
import static org.junit.Assert.assertTrue;
import org.activiti.engine.test.ActivitiRule;
import org.junit.Rule;
import org.junit.Test;

public class TestBasic {
   public ActivitiRule activitiRule = new ActivitiRule();

   public void testLogin() {
      boolean check = activitiRule.getIdentityService().checkPassword("foo", "bar");

When I run the test, I get the following output:
2013-12-14 11:33:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
Information: Loading XML bean definitions from class path resource [activiti.cfg.xml]
2013-12-14 11:33:57 PM org.activiti.engine.impl.db.DbSqlSession executeSchemaResource
INFO: performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
2013-12-14 11:33:57 PM org.activiti.engine.impl.db.DbSqlSession executeSchemaResource
INFO: performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
2013-12-14 11:33:57 PM org.activiti.engine.impl.db.DbSqlSession executeSchemaResource
INFO: performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
2013-12-14 11:33:57 PM org.activiti.engine.impl.ProcessEngineImpl <init>
INFO: ProcessEngine default created

1) H2 database is used instead of production database, hence (correct) activiti.cfg.xml from test directory seams to be used.
2) Process engine "default" is created as an instance of
– but according to test configuration, it should be an instance of
is not called (no log in output) and, consequently, the test fails.

Any ideas? How to activate the
(and previously the right process engine)?

Thank you for any thoughts,
best regards,