AnsweredAssumed Answered

PropertyPlaceholderConfigurer properties not being substituted in unit test

Question asked by osganian on Apr 27, 2015
Latest reply on May 7, 2015 by jbarrez
I'm new to Activiti and I have a simple unit test that is something like:

<java>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:activiti-context.xml", "classpath:test-spring.xml" })
public class SimpleTest {
    /** JUnit test rule to give us access to Activiti services. */
    @Rule
    public ActivitiRule activitiRule = new ActivitiRule("activiti-context.xml");

    @Test
    @Deployment(resources = {"test-process.bpmn20.xml"})
    public void testIt() {
         …
    }
}
<java>

My activiti-context.xml looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-3.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    <!– Activates various annotations to be detected in bean classes –>
    <context:annotation-config />

    <!– Loads properties as spring based properties, things like database
         urls, etc. –>
    <bean id="dbProperties"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreUnresolvablePlaceholders" value="true"/>
        <property name="location" value="classpath:activiti-context.properties" />
    </bean>

    <!–
         DataSource and EntityManager configurations
      –>
    <bean id="activitiDataSource"
          class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${database.activiti.driverClassName}"/>
        <property name="url" value="${database.activiti.url}"/>
        <property name="username" value="${database.activiti.username}"/>
        <property name="password" value="${database.activiti.password}"/>
        <property name="validationQuery" value="${database.activiti.validation-query}"/>
        <!– check for idle connections every 10 mins –>
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <!– setting -1 will make it check all idle connections–>
        <property name="numTestsPerEvictionRun" value="-1" />
        <!– close connections if they are idle for 10 mins –>
        <property name="minEvictableIdleTimeMillis" value="60000" />
    </bean>

    <!–
         Start of Activiti configurations
      –>

    <bean id="transactionManager"
          class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="activitiDataSource" />
    </bean>

    <bean id="processEngineConfiguration"
          class="org.activiti.spring..SpringProcessEngineConfiguration">
        <!– Common configuration properties –>
        <property name="history" value="audit" />
        <property name="jobExecutorActivate" value="true" />

        <!– Database configuration properties –>
        <!– Will create the Activiti tables if they are not present or if they
             are present will update them accordingly. –>
        <property name="dataSource" ref="activitiDataSource" />
        <property name="databaseSchemaUpdate" value="true" />

        <!– –>
        <property name="transactionManager" ref="transactionManager" />

        <!– JPA configuration properties –>
        <property name="jpaHandleTransaction" value="true" />
        <property name="jpaCloseEntityManager" value="true" />
    </bean>

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

    <!– The Activiti suite of services used within the processing engine. –>
    <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>


And my activiti-context.properties where I put my database properties are:


database.activiti.url=jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000
database.activiti.driverClassName=org.h2.Driver
database.activiti.username=sa
database.activiti.password=
database.activiti.validation-query=SELECT 1 FROM DUAL


But when I run my unit test it looks like my database properties are not being evaluated:


12:03:56,801 [main] DEBUG org.springframework.jdbc.datasource.DataSourceUtils  - Fetching JDBC Connection from DataSourc
e
12:03:56,802 [main] ERROR org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl  - Exception while initializing Da
tabase connection
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${database.activiti.driverClassName}'
        at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
        at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
        at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
        at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
        at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(
TransactionAwareDataSourceProxy.java:224)
        at $Proxy10.getMetaData(Unknown Source)
        at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.initDatabaseType(ProcessEngineConfigurationImpl.j
ava:654)
        at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.initDataSource(ProcessEngineConfigurationImpl.jav
a:607)
        at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.init(ProcessEngineConfigurationImpl.java:440)
        at org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl.buildProcessEngine(ProcessEngineConfigurationImpl
.java:412)
        at org.activiti.spring.SpringProcessEngineConfiguration.buildProcessEngine(SpringProcessEngineConfiguration.java
:65)
        at org.activiti.engine.impl.test.TestHelper.getProcessEngine(TestHelper.java:227)
        at org.activiti.engine.test.ActivitiRule.initializeProcessEngine(ActivitiRule.java:237)
        at org.activiti.engine.test.ActivitiRule.starting(ActivitiRule.java:205)
        at org.activiti.engine.test.ActivitiRule.startingQuietly(ActivitiRule.java:171)
        at org.activiti.engine.test.ActivitiRule.access$000(ActivitiRule.java:86)
        at org.activiti.engine.test.ActivitiRule$1.evaluate(ActivitiRule.java:124)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodC
allbacks.java:74)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCal
lbacks.java:82)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCal
lbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallb
acks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.ClassNotFoundException: ${database.activiti.driverClassName}
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
        at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
        … 43 more

Anybody have any idea what I might be doing wrong?

Thanks for any help,
Mike

Outcomes