簡體   English   中英

你如何用JUNIT測試spring jdbcTemplate?

[英]How do you test a spring jdbcTemplate with JUNIT?

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

任何幫助表示贊賞。

謝謝

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

使用Spring測試框架 它允許您的單元測試利用為您的應用程序上下文配置的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>

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

如何使用Spring測試模擬的JNDI數據源?

我沒有使用我的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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM