5

我有一个我正在尝试测试的 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
4

2 回答 2

2

使用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>
于 2012-11-05T00:00:28.113 回答
1

我使用以下链接中的信息解决了我的问题:

如何使用 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>
于 2012-11-05T15:25:40.997 回答