2012-03-28 6 views
2

を春にプロパティを渡すために、私は、設定ファイルと@ContextConfigurationパラメータで春のJUnitテストを持っている:どのように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をプロパティファイルに渡すにはどうすればよいですか?

答えて

2

file:/${CSBI_INTEGRATION_TEST}/*.propertiesに一致するシステムプロパティまたはURLのプロパティを置き換えるPropertyPlaceholderConfigurerを使用しています。

  1. は、このファイルのURLが少し風変わりなthough-に見えるupload.maxsize=[value here]

行が含まれているプロパティが${CSBI_INTEGRATION_TEST}ディレクトリ内のファイルことを確認してください-Dupload.maxsize=[value here]

  • を使用してテストを実行します。これは、あなたのオプションのカップルを与えますなぜ単一の先導/前に${CSBI_INTEGRATION_TEST}

  • 関連する問題