Junit 测试在将 ehcache 添加到 DAO class 后失败,无法在测试中实例化 DAO class

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

我正在将 ehcache 添加到我的项目中,并且我有一个单元测试 class 如果我在我的 DAO 上评论 ehcache 注释,我的 DAO 运行正常,如果我取消注释它们,它会失败,说明它可以不要自动装配 DAO bean。

这是我得到的错误:

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
...

我也试过,不是在单元测试中自动装配 DAO bean,而是自动装配应用程序上下文并按名称获取 bean,当我这样做时,我得到一个异常,它不能投射 $proxy32到我的 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.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 
...

这些是相关文件:

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();
}
...

我不知道还能尝试什么,有什么想法吗?

我正在使用 Spring 4.2.4.RELEASE、Hibernate 5.1.0.Final、Hibernate JPA 1.0.0.Final、ehcache 2.9.0、JUnit 4.12。

这绝对是与 ehcache 相关的东西,因为注释注释使得在测试中自动装配 DAO 成为可能 class,并且单元测试通过了,但我尝试了几个小时,但无法弄清楚。

谢谢!

你的 junit 没有加载你的 spring conf xml。

试试这个:

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

编辑:尝试删除限定符and/or在您的 bean 创建中添加名称属性

第一个错误是因为您试图注入 DragonBallUserDaoJpa 而不是 DragonBallUserDao。为了能够添加缓存层,Spring 在您的 class 上创建了一个代理。而这个代理实现了class(DragonBallUserDao)的接口,然后委托给实际的class(DragonBallUserDaoJpa).

您在检索bean 时遇到了同样的问题。由于代理仅实现接口,因此您不能将其强制转换为实现。所以 ClassCastException.

因此,如果您在测试中这样做(您不需要限定符)

@Autowired
private DragonBallUserDao dragonBallUserDao;

它应该可以解决问题。

另一个解决方案(但我不认为它有用)是强制Spring 使用cglib 来创建代理。这样,您的代理确实会扩展然后具体 class。你需要这样的东西:<aop:aspectj-autoproxy proxy-target-class="true" />

最后,你也可以去掉接口,因为我非常怀疑你有很多这个 DAO 的实现。所以接口是无用的,只是增加噪音。删除它将强制 Spring 创建一个 cglib 代理,因为没有可用的接口。