2012-11-04 147 views
4

我有一個DAO,我正在嘗試使用jdbcTemplate進行測試。 Spring jdbcTemplate上有一個datasoruce屬性,需要設置它才能工作。但是,當JUNIT測試運行時,數據源不存在,並且bean創建失敗。我如何設置jdbcTemplate的數據源在JUNIT測試用例中工作?如何用JUNIT測試spring jdbcTemplate?

任何幫助表示讚賞。

感謝

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'thisDatasource' defined in class path resource [userDataBaseContext.xml]: Invocation of init method failed; nested exception is javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322) 
    ... 33 more 
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial 
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645) 
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) 
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:325) 
    at javax.naming.InitialContext.lookup(InitialContext.java:392) 
    at org.springframework.jndi.JndiTemplate$1.doInContext(JndiTemplate.java:154) 
    at org.springframework.jndi.JndiTemplate.execute(JndiTemplate.java:87) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:152) 
    at org.springframework.jndi.JndiTemplate.lookup(JndiTemplate.java:178) 
    at org.springframework.jndi.JndiLocatorSupport.lookup(JndiLocatorSupport.java:95) 
    at org.springframework.jndi.JndiObjectLocator.lookup(JndiObjectLocator.java:105) 
    at org.springframework.jndi.JndiObjectFactoryBean.lookupWithFallback(JndiObjectFactoryBean.java:201) 
    at org.springframework.jndi.JndiObjectFactoryBean.afterPropertiesSet(JndiObjectFactoryBean.java:187) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417) 
    ... 40 more 

回答

0

我解決了使用從以下鏈接信息我的問題:

How to test a mocked JNDI datasource with Spring?

而不是用我的春天文件中定義的數據源,我剛剛創建了一個新的數據源:

<bean id="thisDatasource" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource" 
p:driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" 
p:url="jdbc:sqlserver://sqlserver:1234;databaseName=myTables" 
p:username="userid" 
p:password="passw0rd" /> 


<bean id="databaseUserDAOTest" 
class="com.spring.security.custom.user.detail.DatabaseUserDAO" > 
<!-- Inject the datasource of the jdbcTemplate --> 
<property name="dataSource" ref="thisDatasource" />   
</bean> 
1

使用Spring Testing Framework。它允許您的單元測試利用爲您的應用程序上下文配置的Spring容器。設置完成後,您可以在數據源上使用@Autowired來注入測試jdbcTemplate所需的數據源。

這是我使用Spring-Data進行測試的一個例子。

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 org.tothought.entities.Post; 
import org.tothought.entities.PostPart; 
import org.tothought.entities.Tag; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
@Transactional 
public class PostRepositoryTest { 

    @Autowired 
    TagRepository tagRepository; 

    @Test 
    public void findOneTest() { 
     Post post = repository.findOne(1); 
     assertNotNull(post); 
     assertTrue(post.getTags().size()>1); 
    } 
} 

注意@ContextConfiguration註釋。這個註解指向用於設置Spring容器的上下文,然後我從中注入我的存儲庫。由於我沒有爲我的上下文指定名稱,因此Spring會在與具有名稱PostRepositoryTest-context.xml的測試類相同的目錄中搜索配置文件。這個設置在上面提供的文檔中有更詳細的描述。

要開始使用該項目,請在您的pom.xml文件中包含以下內容。

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-test</artifactId> 
    <version>3.1.2.RELEASE</version> 
</dependency> 
+0

感謝您對Spring測試框架的細節。我認爲我已經有了「注入」數據源的一切。我遇到的問題是因爲我正在運行JUnit測試,所以數據源不存在。數據源是服務器端元素,Junit測試在服務器外部運行。一個更好的問題可能是,你如何僞造一個用於JDBCTemplate的Junit測試的數據源 –