Greg Dougherty
Greg Dougherty

Reputation: 3471

Cannot resolve reference to bean 'cacheManager'

Trying to run a unit test on a Controller in my Spring app.

My test class:

@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 

The error I'm getting:

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

My application-context-config.xml defines:

<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 Defines:

<ehcache>
    <cache name="backslap"
           maxBytesLocalHeap="256M"
           eternal="false"
           timeToLiveSeconds="3600"
           overflowToDisk="false"
           memoryStoreEvictionPolicy="LRU"/>
</ehcache>

I've got copies of ehcache.xml in three places: src/main/resources/, test/main/resources/ and in the directory of my test class. Nothing fixes this problem.

How do I make Spring find the file during testing (it has no problem finding the file when running)? Failing that, how do I make Spring stop wanting this information (the cache is utterly worthless during my tests), or at least let me just specify it in my application-context-config.xml file?

Thank you for any help.

Upvotes: 3

Views: 12223

Answers (1)

Paul John
Paul John

Reputation: 1661

The error:

 No bean named 'cacheManager' is defined

This error is due to Spring not being able to identify this bean.

Identify the file where this bean is defined, and make sure it's available for the test context.

Upvotes: 0

Related Questions