2013-07-12 45 views
28

我測試下面的DAO使用JUnit自動裝配:春天沒有單元測試使用JUnit

@Repository 
public class MyDao { 

    @Autowired 
    private SessionFactory sessionFactory; 

    // Other stuff here 

} 

正如你所看到的,SessionFactory則是使用Spring自動裝配。當我運行測試時,sessionFactory保持爲空,我得到一個空指針異常。

這是春天SessionFactory的配置:

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

有什麼不對?我怎樣才能爲單元測試啓用自動裝配?

更新:我不知道它是否是運行JUnit測試的唯一方法,但請注意,我正在Eclipse中運行它,右鍵單擊測試文件並選擇「run as」 - >「JUnit test 「

+0

是'包含在掃描路徑MyDao'? – Reimeus

+0

是的,包括在內 – user1883212

+0

''呢?你有你的XML嗎? –

回答

24

添加這樣的事情你的根單元測試類:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 

這將使用XML在默認路徑。如果您需要指定非默認路徑,那麼您可以爲ContextConfiguration註釋提供locations屬性。

http://static.springsource.org/spring/docs/2.5.6/reference/testing.html

+0

我看不到並且包含這些註釋 – user1883212

+0

你是什麼意思? –

+0

好吧我只需要在pom中添加一些東西以便能夠使用註釋。現在我添加了它們,此錯誤:java.lang.IllegalStateException:GenericXmlContextLoader和AnnotationConfigContextLoader都無法檢測到默認值,並且沒有爲Context配置聲明ApplicationContextInitializers [ContextConfigurationAttributes @ 1dd58d8 declaringClass ='org.davis.dao.EmployeeDAOImplTest',locations ='{}', classes ='{}',inheritLocations = true,initializers ='{}',inheritInitializers = true,name = [null],contextLoaderClass ='org.springframework.t ... – user1883212

2

您需要添加註釋的JUnit類,告訴它使用的SpringJunitRunner。你想要的是:

@ContextConfiguration("/test-context.xml") 
@RunWith(SpringJUnit4ClassRunner.class) 

這告訴Junit在你的測試中使用test-context.xml文件在同一個目錄中。這個文件應該與您在春季使用的真正的context.xml類似,但是自然會指向測試資源。

6

您需要使用Spring JUnit運行器才能在您的上下文中連接Spring Bean。下面的代碼假定您有一個名爲testContest.xml的應用程序上下文在測試類路徑中可用。

import org.hibernate.SessionFactory; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.transaction.annotation.Transactional; 

import java.sql.SQLException; 

import static org.hamcrest.MatcherAssert.assertThat; 
import static org.hamcrest.Matchers.startsWith; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"classpath*:**/testContext.xml"}) 
@Transactional 
public class someDaoTest { 

    @Autowired 
    protected SessionFactory sessionFactory; 

    @Test 
    public void testDBSourceIsCorrect() throws SQLException { 
     String databaseProductName = sessionFactory.getCurrentSession() 
       .connection() 
       .getMetaData() 
       .getDatabaseProductName(); 
     assertThat("Test container is pointing at the wrong DB.", databaseProductName, startsWith("HSQL")); 
    } 
} 

注:這適用於春2.5.2的Hibernate 3.6.5

+0

問題是理解正確的使用路徑。我有我的servlet-context.xml在src/main/webapp/WEB-INF/spring-servlet.xml下,我正在使用這個位置:@ContextConfiguration(locations = {「classpath:spring-servlet.xml」})但是這不起作用。我應該寫什麼呢? – user1883212

+0

我的路徑是否正確? – user1883212

+0

我嘗試了所有可能的路徑,這個應用程序有什麼問題? – user1883212

6

的配置可能會導致此背景下丟失文件的位置,一個方法來解決這個問題:

  • 指定ContextConfiguration中的上下文文件位置

,如:

@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" }) 

更多細節

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:META-INF/your-spring-context.xml" }) 
public class UserServiceTest extends AbstractJUnit4SpringContextTests {} 

參考:Thanks to @Xstian