2011-01-28 75 views
4

我一直在嘗試爲我的DAO創建一個JUnit測試,該測試使用Hibernate,並通過Spring在我的applicationContext.xml文件中鏈接到數據庫。如何讓JUnit4註解在Spring2.5中工作?

@RunWith(SpringJUnit4ClassRunner.class) 

否則我得到一個空指針異常的partDao,這是因爲它沒有連接預計:只增加時,會發生

錯誤。

通常我只是說廢棄它並繼續開發沒有JUnit測試,但我寧願開始在我的項目中使用它。

以下是錯誤:

Testsuite: SpecBuilder.webapp.dao.PartDaoTest 
Exception in thread "main" java.lang.NoSuchMethodError: org.junit.runner.notification.RunNotifier.testAborted(Lorg/junit/runner/Description;Ljava/lang/Throwable;)V 
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:146) 
    at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:59) 
    at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:52) 
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34) 
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44) 
    at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:50) 
    at junit.framework.JUnit4TestAdapter.run(JUnit4TestAdapter.java:39) 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.run(JUnitTestRunner.java:515) 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.launch(JUnitTestRunner.java:1031) 
    at org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner.main(JUnitTestRunner.java:888) 
Test SpecBuilder.webapp.dao.PartDaoTest FAILED (crashed) 
/home/zclark/Projects/SpecBuilder/nbproject/build-impl.xml:798: Some tests failed; see details above. 
BUILD FAILED (total time: 2 seconds) 

這裏是我的測試類:

package SpecBuilder.webapp.dao; 

import org.junit.Test; 
import static org.junit.Assert.*; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.junit.runner.RunWith; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.test.context.ContextConfiguration; 

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

    @Autowired 
    PartDao partDao; 

    public PartDaoTest() {} 

    @Test 
    public void autowireTest() { 
     assertNotNull(partDao); 
    } 
} 

這裏是我的applicationContext:

<?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:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> 

    <bean id="propertyConfigurer" 
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
     p:location="/WEB-INF/specbuilder.properties"/> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
     p:driverClassName="${jdbc.driverClassName}" 
     p:url="${jdbc.url}" 
     p:username="${jdbc.username}" 
     p:password="${jdbc.password}" /> 

    <bean id="partDao" class="SpecBuilder.webapp.dao.PartDao"> 
     <property name="sessionFactory" ref="sessionFactory"/> 
    </bean> 

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="annotatedClasses"> 
      <list> 
       <value>SpecBuilder.webapp.model.Part</value> 
      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">${hibernate.dialect}</prop> 
      </props> 
     </property> 
    </bean> 

</beans> 

任何想法?

回答

6

它看起來像你需要JUnit 4.3或4.4來使用Spring 2.5。

+0

謝謝。太盲目了,看不到。做了更多的窺探,https://jira.springframework.org/browse/SPR-5145描述了不兼容的確切原因 – zclark 2011-01-28 03:09:40