0

我有项目 A 中的 dao 层,项目 B 是应用程序层,它使用项目 A 作为它的依赖项

我可以看到项目 A 中的类,并使用这些类在项目 B 中完美编码。

现在我想做 JUnit 测试

在 A (MyTest.java)

@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@ContextConfiguration(locations = { "classpath*:reb/spring/persistenceContextTest.xml", "classpath*:rev/spring/modelContextTest.xml" })
public class MyTest extends AbstractTransactionalJUnit4SpringContextTests {
      @Autowired
      protected HibernateTemplate hibernateTemplate;
      @Autowired
      protected DAOImpl dao;
         //CODE here
}

这里一切都很好。所有属性都已正确注入,没有问题。

现在我需要测试应用层 {project b}

@ContextConfiguration(locations = { "classpath*:reb/spring/applicationContextTest.xml" })
public class MyAnotherTest extends AbstractTransactionalJUnit4SpringContextTests {
     @Autowired
     protected HibernateTemplate hibernateTemplate;
     @Autowired
     protected DAOImpl dao;
}

并在 applicationContextTest.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:jaxws="http://cxf.apache.org/jaxws"
    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://cxf.apache.org/jaxws 
        http://cxf.apache.org/schemas/jaxws.xsd">
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
    <import resource="classpath*:reb/spring/persistenceContextTest.xml" />
    <import resource="classpath*:reb/spring/modelContextTest.xml" />

</beans>

但是当我运行 B 测试时,Spring 无法找到项目 A 中定义的属性。我该如何实现呢?

更新 我将测试 xmls 移动到 Src 文件夹,以便 maven 在项目 B 中识别并且它确实被识别。但是我仍然低于错误

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:308)
    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:220)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:301)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:303)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:240)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:180)
    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.BeanCreationException: Error creating bean with name 'org.apache.cxf.transport.servlet.ServletTransportFactory' defined in class path resource [META-INF/cxf/cxf-servlet.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:580)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:84)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
    at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:280)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:304)
    ... 24 more
Caused by: org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (2) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'bus' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
PropertyAccessException 2: org.springframework.beans.MethodInvocationException: Property 'transportIds' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in URL [file:/C:/Users/Public/CC_DATA/OSS_Mar-2013_SharedDev/OSS/Rosy/JComponents/Rebecca/Rebecca-access/target/classes/rebecca/spring/persistenceContextDump.xml]: Invocation of init method failed; nested exception is org.hibernate.cache.CacheException: net.sf.ehcache.CacheException: Error configuring from null. Initial cause was null
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:102)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
    ... 38 more
4

2 回答 2

2

如果您使用的是 maven,那么其中的任何资源和来源src/test都是单个模块的本地资源,即其中的任何内容projectA/src/test都将从projectB. 有多种选择:

  • 移动测试资源:我不鼓励你这样做,但基本上你可以将你applicationContextTest.xml的移动到src/main文件夹中,但是你的二进制可交付成果中有测试代码。

  • 复制测试资源:当您将任何 bean 设置从复制projectAprojectB. 这通常意味着您正在为单元测试创​​建测试应用程序上下文,并且您的实际应用程序具有不同的应用程序上下文。

  • 删除所有测试应用程序上下文:这将是最干净的解决方案,如果您没有任何测试上下文,您将被迫测试真实的东西。如果您需要特定于环境的属性,您可以使用 PropertyPlaceHolders,如果您需要不同的 bean(连接池等),您可以使用 Profiles。

查看配置文件的新 Spring 3.1 功能:http: //blog.springsource.org/2011/02/14/spring-3-1-m1-introducing-profile/

于 2012-12-11T06:43:04.513 回答
0

您的测试持久性上下文 xml 名称在项目 A 和项目 B 中相同。您可以尝试更改persistenceContextTest.xml项目 A 中的名称persistenceContextTestA.xml以确保问题不是因为文件名重复。

于 2012-12-11T06:51:04.467 回答