1

I'm trying to create a workflow in my web application using the frameworks Activiti and Spring MVC, so I have to Inject Activiti Services by the applicationContext.xml file and then autowire these services.

and this is the problem in the consol after a JUnit Test

GRAVE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@14c8743] to prepare test instance [tds_erp.testSpring@144719c] org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tds_erp.testSpring': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.activiti.engine.RepositoryService tds_erp.testSpring.repositoryService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.activiti.engine.RepositoryService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.activiti.engine.RepositoryService tds_erp.testSpring.repositoryService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.activiti.engine.RepositoryService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.activiti.engine.RepositoryService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

applicatioContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        ">


    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
        <property name="targetDataSource">
            <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
                <property name="username" value="root" />
                <property name="password" value="admin" />
            </bean>
        </property>
    </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="databaseType" value="h2" />
        <property name="dataSource" ref="dataSource" />
        <property name="transactionManager" ref="transactionManager" />
        <property name="databaseSchemaUpdate" value="true" />
        <property name="deploymentResources"
            value="classpath*:chapter4/bookorder.spring.bpmn20.xml" />
        <property name="jobExecutorActivate" value="false" />
    </bean>
    <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
        <property name="processEngineConfiguration" ref="processEngineConfiguration" />
    </bean>
    <bean id="repositoryService" factory-bean="processEngine"
        factory-method="getRepositoryService" />
    <bean id="runtimeService" factory-bean="processEngine"
        factory-method="getRuntimeService" />
    <bean id="taskService" factory-bean="processEngine"
        factory-method="getTaskService" />
    <bean id="historyService" factory-bean="processEngine"
        factory-method="getHistoryService" />
    <bean id="managementService" factory-bean="processEngine"
        factory-method="getManagementService" />
</beans>

the JUnit Test file:

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.Map;

import org.activiti.bpmn.model.Task;
import org.activiti.engine.FormService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.IdentityService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:applicatioContext.xml")

public class testSpring {

    @Autowired(required=true)
     RepositoryService repositoryService;
    @Autowired(required=true)
     RuntimeService runtimeService;
    @Autowired(required=true)
     TaskService taskService;


    @Test
    public void simpleSpringTest() {

    Map<String, Object> variableMap =
    new HashMap<String, Object>();
    variableMap.put("object", "Activiti in Action");
    variableMap.put("description", "123456");
    runtimeService.startProcessInstanceByKey(
    "employeeRequest", variableMap);

    Task task = (Task) taskService
    .createTaskQuery().taskAssignee("kermit")
    .singleResult();
    assertEquals("Complete order", task.getName());
    taskService.complete(task.getId());
    assertEquals(0, runtimeService.
    createProcessInstanceQuery().count());
    }
    }

help me please !

Spartan
  • 1,167
  • 4
  • 20
  • 40

1 Answers1

0

It looks like you want to use beans defined in applicatioContext.xml like repositoryService etc in your test case. But all you are declaring in the @ContextConfiguration is the activiti's default config file activiti.cfg.xml Can you try the below in your test:

@ContextConfiguration("classpath:applicatioContext.xml")

Just a suggestion: it may be a good idea to create a separate applicatioContext.xml for test context similar to the one in webapplicationContext. This will help keep a different data etc where you can manage pre/post conditions of data better than your normal development DB instance. You may even want to spawn a DB instance for the test itself and tear down after the test.

Edits: You can give multiple conf files like this - @ContextConfiguration(locations = { "classpath:applicatioContext.xml" , "classpath*:activiti.cfg.xml"}). check this post.

generally defining "<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">" should load the activiti.cfg.xml internally unless you have changed its default location.

Community
  • 1
  • 1
Bharath
  • 259
  • 1
  • 13
  • thanx for answer, but i have 2 configuration files, `applicatioContext.xml` and `activiti.cfg.xml` and i tried them but the same problem – Spartan Apr 08 '15 at 10:05
  • i tried your suggestion, but i dont know how can i call the activiti config after the separation from the applicationContext. it says there is no bean defined – Spartan Apr 08 '15 at 10:20
  • you can give multiple conf files like this - @ContextConfiguration(locations = { "classpath:applicatioContext.xml" , "classpath*:activiti.cfg.xml"}). check this post - http://stackoverflow.com/questions/4377699/spring-contextconfiguration-how-to-put-the-right-location-for-the-xml – Bharath Apr 08 '15 at 12:17
  • generally defining "``" should load the activiti.cfg.xml internally unless you have changed its default location – Bharath Apr 08 '15 at 12:18
  • 1
    thank you for your answer, its resolved by placing this line `` at the first of the Activiti configuration, so i understand that the ProcessEngine should be loaded as the first one, order is important – Spartan Apr 08 '15 at 16:32
  • 1
    I am glad it worked. From what I know the order of bean definition should not matter. i think it was something else that caused the problem. But i cant say what with limited information of what you fixed :) . But happy to help. – Bharath Apr 08 '15 at 17:41
  • 1
    I have edited the answer from comments. can you accept the answer? – Bharath Apr 08 '15 at 19:39