简体   繁体   中英

Junit test failing after adding ehcache to a DAO class, can't instantiate DAO in test class

I'm adding ehcache to my project and I have a unit test class for my DAO that runs OK if I comment the ehcache annotations on my DAO and it fails if I uncomment them, stating it can't autowire the DAO bean.

This is the error I get:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.nicobrest.kamehouse.dao.DragonBallUserDaoJpaTest': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nicobrest.kamehouse.dao.DragonBallUserDaoJpa com.nicobrest.kamehouse.dao.DragonBallUserDaoJpaTest.dragonBallUserDaoJpa;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nicobrest.kamehouse.dao.DragonBallUserDaoJpa] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true), @org.springframework.beans.factory.annotation.Qualifier(value=dragonBallUserDaoJpa)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at
...

I also tried, instead of autowiring the DAO bean in the unit test, autowiring the application context and getting the bean by name, and when I do that, I get an exception that it can't cast a $proxy32 to my DAO class.

java.lang.ClassCastException: com.sun.proxy.$Proxy32 cannot be cast to com.nicobrest.kamehouse.dao.DragonBallUserDaoJpa
    at com.nicobrest.kamehouse.dao.DragonBallUserDaoJpaTest.setUp(DragonBallUserDaoJpaTest.java:71)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at 
...

These are the relevant files:

applicationContext.xml

...
<cache:annotation-driven cache-manager="cacheManager"/>
<import resource="applicationContext-persistence.xml" />

<!-- Ehcache configuration -->
<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>

<!-- Example endpoints beans -->
<bean id="dragonBallUserService" class="com.nicobrest.kamehouse.service.DragonBallUserService">
  <property name="dragonBallUserDao" ref="dragonBallUserDaoJpa" />
</bean>

<bean id="dragonBallUserDaoJpa" class="com.nicobrest.kamehouse.dao.DragonBallUserDaoJpa">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
...

DragonBallUserDaoJpaTest.java

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class DragonBallUserDaoJpaTest {

private static final Logger LOGGER 
LoggerFactory.getLogger(DragonBallUserDaoJpaTest.class);

@Autowired
@Qualifier("dragonBallUserDaoJpa")
private DragonBallUserDaoJpa dragonBallUserDaoJpa;
...
@Test
public void createDragonBallUserTest() {

  DragonBallUser dragonBallUser = new DragonBallUser(null, "vegeta", "vegeta@dbz.com", 49, 40,
      1000);

  try {
    assertEquals(0, dragonBallUserDaoJpa.getAllDragonBallUsers().size());
    dragonBallUserDaoJpa.createDragonBallUser(dragonBallUser);
    assertEquals(1, dragonBallUserDaoJpa.getAllDragonBallUsers().size());
    dragonBallUserDaoJpa
        .deleteDragonBallUser(dragonBallUserDaoJpa.getDragonBallUser("vegeta").getId());
  } catch (KameHouseBadRequestException | KameHouseNotFoundException e) {
    e.printStackTrace();
    fail("Caught unexpected exception.");
  }
}
...

DragonBallUserDaoJpa.java

public class DragonBallUserDaoJpa implements DragonBallUserDao {

@Autowired
private EntityManagerFactory entityManagerFactory;
...
@CacheEvict(value = { "getAllDragonBallUsersCache" }, allEntries = true)
public Long createDragonBallUser(DragonBallUser dragonBallUser) {

  EntityManager em = getEntityManager();
  try {
    em.getTransaction().begin();
    em.persist(dragonBallUser);
    em.getTransaction().commit();
  } catch (PersistenceException pe) {
    ...
  } finally {
    em.close();
  }
  return dragonBallUser.getId();
}
...

I don't know what else to try, any ideas?

I'm using Spring 4.2.4.RELEASE, Hibernate 5.1.0.Final, Hibernate JPA 1.0.0.Final, ehcache 2.9.0, JUnit 4.12.

It's definitely something related to ehcache because commenting the annotations makes it possible to autowire the DAO in the test class, and the unit tests pass but I tried for hours and can't figure it out.

Thanks!

The first error is because you are trying to inject to DragonBallUserDaoJpa instead of DragonBallUserDao . To be able to add the caching layer, Spring creates a proxy over your class. And this proxy implements the interface of the class ( DragonBallUserDao ) and then delegates to the actual class ( DragonBallUserDaoJpa ).

You have the same problem when retrieving the bean. Since the proxy is only implementing the interface, you can't cast it to the implementation. So ClassCastException .

So if you do this in your test (you don't need the qualifier)

@Autowired
private DragonBallUserDao dragonBallUserDao;

It should do the trick.

Another solution (but I don't think it is useful) is to force Spring to use cglib to create proxies. That way, your proxies will indeed extend then concrete class. You need something like this: <aop:aspectj-autoproxy proxy-target-class="true" />

Finally, you can also get rid of the interface because I highly doubt you are having many implementation of this DAO. So the interface is useless and just add noise. Removing it will force Spring to create a cglib proxy since no interface will be available.

Your junit doesn't load your spring conf xml.

Try this :

@ContextConfiguration(locations = {
    "classpath:pathTo/applicationContext.xml"})

Edit: try removing the qualifier and/or add the name attribute in your bean creation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM