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="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="testDataGenerator" class="cdq.cdl.processes.TestDataGenerator"
      init-method="init">
      <property name="processEngine" ref="processEngine" />
   </bean>
   
   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
      <property name="url" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
   </bean>
   
   <bean id="transactionManager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" />
   </bean>

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

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


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();

      LOGGER.info("Initializing test users");
      initDemoUsers();
   }

   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);
         user.setPassword(password);
         identityService.saveUser(user);
      }
   }
}


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 {
   
   @Rule
   public ActivitiRule activitiRule = new ActivitiRule();

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


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


Observations:
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
org.activiti.engine.impl.ProcessEngineImpl
– but according to test configuration, it should be an instance of
org.activiti.spring.ProcessEngineFactoryBean
.
3)
initi
from
TestDataGenerator
is not called (no log in output) and, consequently, the test fails.

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

Thank you for any thoughts,
best regards,
kai

Outcomes