Comment tester un printemps jdbcTemplate avec JUNIT?
J'ai un DAO que je suis en train de test qui utilise un jdbcTemplate. Le printemps jdbcTemplate a un datasoruce attribut doit être définie pour que cela fonctionne. Toutefois, lorsqu'un test JUNIT pistes de la source de données n'existe pas et le haricot création échoue. Comment puis-je configurer la source de données de la jdbcTemplate de travail dans le cas de test JUNIT?
Toute aide est appréciée.
Grâce
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
OriginalL'auteur E Paiz | 2012-11-04
Vous devez vous connecter pour publier un commentaire.
Utiliser le Spring Framework De Test. Il permet à votre appareil de test de l'effet de levier d'un Printemps Conteneur configuré pour votre application-contexte. Une fois la configuration, vous pouvez utiliser @Autocâblés sur votre source de données à injecter de la source de données nécessaires pour tester les jdbcTemplate.
Voici un exemple d'un de mes test à l'aide de Spring-Data.
Avis de la
@ContextConfiguration
annotation. Cette annotation points pour le contexte utilisé pour configurer le Printemps conteneur, que j'ai ensuite injecter mon référentiel. Depuis, je n'ai pas spécifié un nom pour mon contexte Spring recherche pour l'un fichier de configuration dans le même répertoire que mon test de la classe qui a le nom PostRepositoryTest-context.xml. Cette configuration est décrite plus en détail dans la documentation fournie ci-dessus.Pour commencer à utiliser le projet d'inclure les éléments suivants dans votre pom.xml fichier.
OriginalL'auteur Kevin Bowersox
J'ai résolu mon problème en utilisant des informations à partir du lien suivant:
Comment tester une moqué datasource JNDI avec le Printemps?
Au lieu d'utiliser la source de données définie dans mon printemps fichiers, je viens de créer une nouvelle source de données:
OriginalL'auteur E Paiz