2012-09-05 77 views
0

我正在使用Hibernate和Spring。 Mys DAO正在我的應用程序中工作。 我需要現在創建測試,但我遇到了問題。如何在使用JUNIT的DAO測試中使用依賴注入?

這是我的課測試:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"classpath:applicationContext-test.xml"}) 
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class}) 
public class MunicipioDaoTest { 

    @Autowired 
    private MunicipioDAO municipioDAO; 

    @Test 
    public void testCount() { 
     ...  
     municipioDAO.count(); 
     ... 
    } 

    @Test 
    public void testGet() { 
     ... 
     municipioDAO.get(1); 
     ... 
    } 

的testCount()的作品,但testGet()我得到這個異常:

org.hibernate.MappingException: Unknown entity: com.eagle.eagleservice.model.entity.Municipio 
at org.hibernate.internal.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:1192) 
at org.hibernate.internal.SessionImpl.locateEntityPersister(SessionImpl.java:2436) 
at org.hibernate.internal.SessionImpl.access$2100(SessionImpl.java:172) 
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2378) 
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2382) 
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.<init>(SessionImpl.java:2369) 
at org.hibernate.internal.SessionImpl.byId(SessionImpl.java:1052) 
at org.hibernate.internal.SessionImpl.load(SessionImpl.java:967) 
at com.eagle.core.model.dao.AbstractDAO.get(AbstractDAO.java:104) 
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:319) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) 
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110) 
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
at $Proxy56.get(Unknown Source) 
at com.eagle.eagleservice.model.dao.MunicipioDaoTest.testFind(MunicipioDaoTest.java:55) 
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:45) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74) 
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83) 
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) 
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:300) 
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) 

這是我applicationContext-test.xml

<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:tx="http://www.springframework.org/schema/tx" 
xmlns:zksp="http://www.zkoss.org/2008/zkspring/core" 
xmlns:jee="http://www.springframework.org/schema/jee" 
xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd 
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd 
    http://www.zkoss.org/2008/zkspring/core http://www.zkoss.org/2008/zkspring/core/zkspring-core.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

<context:annotation-config /> 
<context:component-scan base-package="com.eagle.core, com.eagle.eagleservice" /> 

<!-- Enables ZK custom scopes for Spring Beans --> 
<zksp:zk-config/> 

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
    <property name="driverClassName" value="org.postgresql.Driver" /> 
    <property name="url" value="jdbc:postgresql://servidor:5432/eagleservice_tu" /> 
    <property name="username" value="admin" /> 
    <property name="password" value="admin" /> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" value="com.eagle.core.model, com.eagle.eagleservice.model" /> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="javax.persistence.validation.mode">auto</prop> 

      <!-- Habilita a alteração do banco de dados automaticamente sempre que o mapeamento das entidades forem alteradas --> 
      <prop key="hibernate.hbm2ddl.auto">update</prop>    
     </props> 
    </property> 
</bean> 

<!-- A transaction manager for working with Hibernate SessionFactories --> 
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

<tx:annotation-driven transaction-manager="transactionManager" /> 

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:org/springframework/security/messages"/> 
</bean> 

這裏有什麼問題?

回答

1

Spring運行正常,DAO正在注入測試。

錯誤Unknown entity: com.eagle.eagleservice.model.entity.Municipio是由於Hibernate無法找到DAO正在使用的Municipo類。

貌似的sessionFactory豆不掃描包com.eagle.eagleservice.model.entityMunicipio實體是英寸

在sesionFactory豆,更改

<property name="packagesToScan" value="com.eagle.core.model, com.eagle.eagleservice.model" /> 

要包括com.eagle.eagleservice.model.entity

<property name="packagesToScan" value="com.eagle.core.model, com.eagle.eagleservice.model, com.eagle.eagleservice.model.entity" /> 
+0

我試過了,但沒有奏效。我犯了同樣的錯誤。 –

0

packagesToScan不包含實體類所在的包:com.eagle.eagleservice.model.entity

+0

我試過了,但沒有奏效。我犯了同樣的錯誤。 –