2012-01-31 37 views
2

我试图设置Junit测试来测试JPA配置。该类如下:春季JPA道 - 无法自动填充字段

@ContextConfiguration 
@RunWith(SpringJUnit4ClassRunner.class) 
public class OrderPersistenceTests { 
    @Autowired 
    public JpaDaoRole dr; 

    @Transactional 
    public void test1() throws Exception { 
     Role r = new Role(); 
     r.setIdRole(18); 
     r.setDescription("description"); 
     dr.persist(r); 
     Role r1=dr.findById(r.getIdRole()); 
     assertEquals(r.getIdRole(), r1.getIdRole()); 
    } 
} 

这里也是我的应用程序上下文配置:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <description>Example configuration to get you started.</description> 

    <!-- Generic --> 
    <context:annotation-config /> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 
<context:component-scan base-package="com.windy.spring" /> 

    <!-- JPA --> 

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> 
    <tx:annotation-driven /> 
    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> <!-- Prints used SQL to stdout --> 
       <property name="generateDdl" value="true" /> <!-- Generates tables. --> 
       <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" /> 
      </bean> 
     </property> 
    </bean> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver"/> 
     <property name="url" value="jdbc:mysql://localhost:3306/windydb"/> 
     <property name="username" value="windyuser"/> 
     <property name="password" value="123"/> 
    </bean> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 


</beans> 

当我尝试启动我的测试,我从我的日志中看到的是,实体类加载正确,但dao类不能自动装配。我的道实施如下:

@Repository 
public class JpaDaoRole extends JpaDao<Role, Integer> implements DaoRole { 

} 

public abstract class JpaDao <E, K> implements Dao<E, K> { 
    protected Class<E> entityClass; 

    private static final Logger logger = LoggerFactory.getLogger(JpaDao.class); 

    @PersistenceContext 
    protected EntityManager entityManager; 

    public JpaDao() { 
     ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass(); 
     this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[0]; 
     logger.info(this.entityClass.toString()); 
    } 

    public EntityManager name() { 
     return entityManager; 
    } 

    public void persist(E entity) { entityManager.persist(entity); } 

    public void remove(E entity) { entityManager.remove(entity); } 

    public E findById(K id) { return entityManager.find(entityClass, id); } 
} 

日志我STHE如下:

2012-01-31 00:09:49,456 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Pre-instantiating singletons in org.s[email protected]3a747fa2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,jpaDaoRole,jpaDaoUser,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,dataSource,transactionManager]; root of factory hierarchy> 
2012-01-31 00:09:49,492 INFO [com.windy.spring.JpaDao] - <class com.windy.spring.Role> 
2012-01-31 00:09:49,538 INFO [com.windy.spring.JpaDao] - <class com.windy.spring.User> 
2012-01-31 00:09:49,585 ERROR [org.springframework.test.context.TestContextManager] - <Caught exception while allowing TestExecutionListener [org.springframewor[email protected]629ca1fb] to prepare test instance [[email protected]]> 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.windy.spring.OrderPersistenceTests': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.windy.spring.JpaDaoRole com.windy.spring.OrderPersistenceTests.dr; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.windy.spring.JpaDaoRole] 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)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:288) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1074) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110) 
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75) 
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) 
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50) 
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) 
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174) 
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.windy.spring.JpaDaoRole com.windy.spring.OrderPersistenceTests.dr; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.windy.spring.JpaDaoRole] 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)} 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285) 
    ... 26 more 
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.windy.spring.JpaDaoRole] 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)} 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
    ... 28 more 
2012-01-31 00:09:49,592 INFO [org.springframework.context.support.GenericApplicationContext] - <Closing [email protected]b8bef7: startup date [Tue Jan 31 00:09:47 CET 2012]; root of context hierarchy> 
2012-01-31 00:09:49,593 INFO [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <Destroying singletons in org.s[email protected]3a747fa2: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,jpaDaoRole,jpaDaoUser,org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor#0,org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0,entityManagerFactory,dataSource,transactionManager]; root of factory hierarchy> 
2012-01-31 00:09:49,603 INFO [org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean] - <Closing JPA EntityManagerFactory for persistence unit 'application'> 
2012-01-31 00:09:49,603 INFO [org.hibernate.impl.SessionFactoryImpl] - <closing> 

我无法理解,因为如果我宣布CLA jpadao作为@Repository不自动装配。

在此先感谢您的帮助

达尼洛

回答

6

我想在OrderPersistenceTests中,您应该使用接口DaoRole而不是实际的类。

@Autowired 
public DaoRole dr; 
+0

非常感谢你对我应该使用通用实现。 – ddelizia 2012-02-01 21:10:23

+0

有同样的问题,你的策略解决了它。处理代理时,我总是忘记使用接口。非常感谢! – MaVVamaldo 2012-10-20 09:29:15

0

我相信你有@ContextConfiguration来注释测试类,并告诉春天在配置XML位于:

@ContextConfiguration(locations = {"classpath:app-context.xml"}) 
@RunWith(SpringJUnit4ClassRunner.class) 
public class OrderPersistenceTests { 
    ... 
} 
+0

我添加的地点,以XML上下文@ContextConfiguration(“APP-context.xml的”),但我仍然得到同样的错误。 – ddelizia 2012-01-31 22:16:50

+0

我尝试以包含所有Dao逻辑的方式修改JpaDaoRole类(所有方法都直接在类中实现,因此它不实现/扩展任何类),我的测试正在运行。我的东西不喜欢这个结构@Repository public class JpaDaoRole extends JpaDao 实现DaoRole { } 我不明白为什么?任何帮助? – ddelizia 2012-01-31 23:34:45