在Spring Integeration测试环境

时间:2016-11-10 08:48:45

标签: java spring-mvc testing spring-test

我正在为Spring Web应用程序设置测试环境。我使用.properties个文件和Environment来配置我的持久层属性,如下所示(为简洁起见,我跳过了代码):

PersistenceConfig.java

@PropertySources({
@PropertySource(value= "classpath:/com/hp/properties/persistence/mysqldb.properties"),
@PropertySource(value = "classpath:/com/hp/properties/persistence/hibernate.properties")})
public class PersistenceConfig {

@Autowired
Environment env;

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(new String[] {"com.hp.model.entity"});

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    em.setJpaVendorAdapter(vendorAdapter);
    em.setJpaProperties(hibernateProperties());

    return em;
}

private Properties hibernateProperties(){
    Properties properties = new Properties();
    properties.setProperty("hibernate.dialect", env.getRequiredProperty("hibernate.dialect.mysql"));
    properties.setProperty("hibernate.show_sql", env.getRequiredProperty("hibernate.showsql"));
    properties.setProperty("hibernate.format_sql", env.getRequiredProperty("hibernate.formatsql"));
    return properties;
}
}

我正在使用Spring WebApplicationContext创建我的测试类,如下所示:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes={ApplicationConfig.class})
@WebAppConfiguration
@TestPropertySource(value = {"classpath:/com/hp/properties/persistence/mysqldb.properties", 
    "classpath:/com/hp/properties/persistence/hibernate.properties"})
public class PatientServiceTest {

MockMvc mockMvc;

@Autowired
WebApplicationContext webApplicationContext;

@Before
public void setUp(){
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}

@Test
public void testForDependentBeans(){
    Assert.assertThat(webApplicationContext.containsBean("patientRepository"), Matchers.is(true));
}

}

但是当我运行测试时,由于找不到PersistnceConfig而导致hibernate.properties的初始化出错。我还尝试将这些文件从src/main/java复制到src/test/java并添加@TestPropertySource以明确提供属性文件。然而,我得到了同样的错误。如果它有帮助,下面是堆栈跟踪。

另外,如何在Spring整合测试中加载特定于测试环境的不同属性文件,而不是我的生产。

  

java.lang.IllegalStateException:无法加载ApplicationContext       at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)       at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)       在org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)       在org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)       在org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)       在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)       在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)       在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)       在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)       在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)       在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)       在org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)       在org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)       在org.junit.runners.ParentRunner.run(ParentRunner.java:363)       在org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)       在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)       在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)       在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)   引起:org.springframework.beans.factory.BeanCreationException:创建名称为' entityManagerFactory'的bean时出错在类路径资源[com / hp / config / PersistenceConfig.class]中定义:init方法的调用失败;嵌套异常是java.lang.NoSuchMethodError:javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax / persistence / ValidationMode;       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)       at org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)       在org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1076)       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:851)       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)       at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:134)       at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:61)       在org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)       在org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)       at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)       在org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)       ......还有25个   引起:java.lang.NoSuchMethodError:javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax / persistence / ValidationMode;

0 个答案:

没有答案