尝试在我的 Spring 应用程序中的 Controller 上运行单元测试。
我的测试类:
@RunWith (SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration ({"classpath*:WEB-INF/spring/application-context-config.xml", "data-source-test-config.xml",
"classpath*:WEB-INF/spring/security-context-config.xml"})
public class PropsControllerTest
我得到的错误:
2015-04-03 23:22:51.729 WARN AbstractApplicationContext:487 - Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.interceptor.CacheInterceptor#0': Cannot resolve reference to bean 'cacheManager' while setting bean property 'cacheManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'cacheManager' is defined
我的 application-context-config.xml 定义:
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"/>
</bean>
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
<property name="shared" value="true"/>
</bean>
ehcache.xml 定义:
<ehcache>
<cache name="backslap"
maxBytesLocalHeap="256M"
eternal="false"
timeToLiveSeconds="3600"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>
我在三个地方获得了 ehcache.xml 的副本:src/main/resources/、test/main/resources/和我的测试类目录。没有什么可以解决这个问题。
如何让Spring在测试时找文件(运行时找文件没问题)?如果做不到这一点,我如何让 Spring 不再需要此信息(缓存在我的测试期间完全没有值(value)),或者至少让我只在我的 application-context-config.xml 文件中指定它?
感谢您的帮助。
最佳答案
错误:
No bean named 'cacheManager' is defined
此错误是由于 Spring 无法识别此 bean。
确定定义此 bean 的文件,并确保它可用于测试上下文。
关于java - 无法解析对 bean 'cacheManager' 的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442785/