gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:38:40 25 4
gpt4 key购买 nike

我正在将 ehcache 添加到我的项目中,并且我的 DAO 有一个单元测试类,如果我在 DAO 上注释 ehcache 注释,则运行正常,如果我取消注释它们,则运行失败,指出它无法 Autowiring 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
...

我也尝试过,不是在单元测试中 Autowiring DAO bean,而是 Autowiring 应用程序上下文并按名称获取 bean,当我这样做时,我得到一个异常,它无法将 $proxy32 转换为我的DAO 类。

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

这些是相关文件:

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 有关,因为注释注释使得可以在测试类中 Autowiring DAO,并且单元测试通过了,但我尝试了几个小时但无法弄清楚。

谢谢!

最佳答案

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

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

因此,如果您在测试中执行此操作(则不需要限定符)

@Autowired
private DragonBallUserDao dragonBallUserDao;

应该可以解决问题。

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

最后,您还可以摆脱该接口(interface),因为我非常怀疑您是否拥有该 DAO 的许多实现。所以这个接口(interface)毫无用处,只是增加噪音。删除它会强制 Spring 创建一个 cglib 代理,因为没有可用的接口(interface)。

关于java - 将 ehcache 添加到 DAO 类后,Junit 测试失败,无法在测试类中实例化 DAO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45841047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com