How do you test spring jdbcTemplate with JUNIT?

I have a DAO that I am trying to verify using jdbcTemplate. spring jdbcTemplate has a datasoruce attribute that must be set for it to work. However, when the data source is running in the JUNIT test, it does not exist and the creation of the bean fails. How can I configure the jdbcTemplate data source to work in a JUNIT test case?

Any help is appreciated.

thanks

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
source share
2 answers

I solved the problem using the information at the following link:

How to test a mocking JNDI data source using Spring?

Instead of using the data source defined in my spring files, I just created a new data source:

 <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> 
0
source

Use the Spring Framework for testing . This allows unit test to use the Spring container configured for your application context. Once configured, you can use @Autowired on your data source to enter the data source required for testing jdbcTemplate.

Here is an example of one of my tests using 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); } } 

Note the annotation @ContextConfiguration . This annotation points to the context used to configure the Spring container, from which I then paste my repository. Since I did not specify a name for my context, Spring is looking for a configuration file in the same directory as my test class named PostRepositoryTest-context.xml. This setting is described in more detail in the above documentation.

To start using the project, include the following in the pom.xml file.

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

Source: https://habr.com/ru/post/1444012/


All Articles