mardi 28 avril 2015

why the @cacheable annotation causing unit test to fail

I am using Spring 3.2.1 and followed this link from Spring to set up the EH caching in my application. Before setting up the ehcache, my unit tests were working. They even work when I comment the @cacheable annotation from the code. applicationContext.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
    xmlns:xsi="http://ift.tt/ra1lAU" xmlns:aop="http://ift.tt/OpNdV1"
    xmlns:context="http://ift.tt/GArMu7" xmlns:p="http://ift.tt/1jdM0fE"
    xmlns:cache="http://ift.tt/LO4PtR"
    xsi:schemaLocation="
    http://ift.tt/GArMu6 http://ift.tt/18sW2ax
    http://ift.tt/GArMu7 http://ift.tt/1bGeTcI
    http://ift.tt/LO4PtR http://ift.tt/1Gvmjlz">

        <!-- Setting to activate caching annotations -->
    <cache:annotation-driven cache-manager="cacheManager"/>

    <!-- Setting to activate annotation based configurations. The base-package 
        will be scanned to find and register beans. -->
    <context:component-scan base-package="com.api" />

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"
        p:cache-manager-ref="ehcache" />

    <!-- EhCache library setup -->
    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
        p:config-location="ehcache.xml" />
</beans>

textContext.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://ift.tt/GArMu6"
    xmlns:xsi="http://ift.tt/ra1lAU" xmlns:context="http://ift.tt/GArMu7"
    xsi:schemaLocation="
    http://ift.tt/GArMu6 http://ift.tt/18sW2ax
    http://ift.tt/GArMu7 http://ift.tt/1bGeTcI">

    <import resource="classpath:applicationContext.xml"/>

</beans> 

ehcache.xml:

<ehcache xmlns:xsi="http://ift.tt/ra1lAU"
    xsi:noNamespaceSchemaLocation="ehcache.xsd" 
    updateCheck="false"
    monitoring="autodetect" 
    dynamicConfig="true">

    <cache name="myCache" 
        maxEntriesLocalHeap="10000"
        eternal="false" 
        timeToIdleSeconds="14400" timeToLiveSeconds="86400"
        memoryStoreEvictionPolicy="LFU" 
        transactionalMode="off">
        <persistence strategy="none" />
    </cache>

</ehcache>

Test.java:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/testContext.xml" })
public class WordPressServiceImplIntegrationTest {

  @Test
    public void testGetObjectFaqIntegration() {
        try {
            ResponseObj faq = service.getObject(baseURL + REST_OPERATION,
                    ResponseObj.class);
            Assert.assertNotNull(faq);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

   @Autowired
    private ServiceAPI service;

ServiceAPI.java

@Repository
public class ServiceAPI implements IServiceAPI {
   @Cacheable(value="mycache")
    public <T> T getObject(String url, Class<T> responseType) {

        return restClient.getObject(url, responseType);
    }

}

If I comment out the @cacheable in my ServiceAPI.java. My JUnit test starts to work. Otherwise I get the following error:

    Could not autowire field: private com.api.ServiceAPI com.api.test.ServiceAPITest.service;nested exception is
 org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type [com.api.ServiceAPI] found for dependency: expected 
at least 1 bean which qualifies as autowire candidate for this dependency.

Aucun commentaire:

Enregistrer un commentaire