我有一个正在尝试测试的 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 Testing Framework .它允许您的单元测试利用为您的应用程序上下文配置的 Spring 容器。设置完成后,您可以在数据源上使用@Autowired 来注入(inject)测试 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 容器的上下文,然后我从中注入(inject)我的存储库。由于我没有为我的上下文指定名称,因此 Spring 在与我的测试类相同的目录中搜索名为 PostRepositoryTest-context.xml 的配置文件。上面提供的文档中更详细地描述了此设置。
要开始使用该项目,请在您的 pom.xml 文件中包含以下内容。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
关于java - 您如何使用 JUNIT 测试 spring jdbcTemplate?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13224036/