如何将属性传递给spring junit测试配置

时间:2012-03-28 08:48:01

标签: spring parameters junit

我有一个带有配置文件和@ContextConfiguration参数的Spring JUnit测试:

package ru.csbi.registry.domain.envers.integration;

import org.hibernate.Transaction;
import org.junit.Test;
import org.junit.experimental.categories.Category;
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;

import ru.csbi.registry.domain.property.Building;
import ru.csbi.registry.utils.PersistenceManagerHibernate;
import ru.csbi.test.catergory.integration.container.IntegrationTestsRequiringBothSpringIoCContainerAndTestDatabaseCategory;

@Category(IntegrationTestsRequiringBothSpringIoCContainerAndTestDatabaseCategory.class)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {  "app-config.xml",
                                 "mvc-config.xml",
                                 "aop-config.xml",
                                 "ds-config.xml",
                                 "security-acl-config.xml",
                                 "security-authentication-config.xml" })
//@TransactionConfiguration
//@Transactional
//@ActiveProfiles({"dev", "integration"})
public class RegressionTest {

@Autowired
private PersistenceManagerHibernate persistenceManagerHibernate;

/**
 * Regression test for MR-1088.
 */
@Test
public void test() {
    Building building = createBuildingDetachedObject();
    Transaction transaction = persistenceManagerHibernate.getSessionFactory().getCurrentSession().getTransaction();
    transaction.begin();
    persistenceManagerHibernate.save(building);
    transaction.commit();
    Transaction transaction2 = persistenceManagerHibernate.getSessionFactory().getCurrentSession().getTransaction();
    transaction2.begin();
    persistenceManagerHibernate.delete(building);
    transaction2.commit();
}

private Building createBuildingDetachedObject() {
    Building building = new Building();
    building.setId(1L);
    return building;
}
}

配置文件使用* .properties文件中的属性:

<?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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

<bean id="servletContext" class="org.springframework.mock.web.MockServletContext" />

<bean id="propertyConfigurer"       class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchSystemEnvironment" value="true" />
    <property name="locations">
        <list>
            <value>file:/${CSBI_INTEGRATION_TEST}/*.properties</value>
        </list>
    </property>
    <!-- <property name="order" value="0" /> -->
</bean>

<!-- Scans within the base package of the application for @Components to 
    configure as beans -->
<context:component-scan base-package="ru.csbi.registry" />
<!-- Configure the multipart resolver -->
<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- one of the properties available; the maximum file size in bytes -->
    <property name="maxUploadSize" value="${upload.maxsize}" />
</bean>

并且测试执行因错误而失败:

 java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:157)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
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)
 Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: **Invalid bean definition with name 'multipartResolver' defined in class path resource [ru/csbi/registry/domain/envers/integration/app-config.xml]: Could not resolve placeholder 'upload.maxsize'**
    at org.springframework.beans.factory.config.PlaceholderConfigurerSupport.doProcessProperties(PlaceholderConfigurerSupport.java:209)
at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:220)
at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:84)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:656)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:446)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
... 24 more


Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: **Invalid bean definition with name 'multipartResolver' defined in class path resource [ru/csbi/registry/domain/envers/integration/app-config.xml]: Could not resolve placeholder 'upload.maxsize'**

如何将参数upload.maxsize传递给属性文件?

1 个答案:

答案 0 :(得分:2)

您使用的是PropertyPlaceholderConfigurer,它将替换在系统属性或匹配file:/${CSBI_INTEGRATION_TEST}/*.properties的网址中找到的属性。这为您提供了几个选项:

  1. 使用-Dupload.maxsize=[value here]
  2. 运行测试
  3. 确保${CSBI_INTEGRATION_TEST}目录中的属性文件包含一行upload.maxsize=[value here]
  4. 这个文件网址看起来有点古怪 - 为什么单个领先/前${CSBI_INTEGRATION_TEST}