How to get jndi environment variables with junit test in Spring?

Dalton Dias

i have a problem with JNDI for test my application, not is the datasource, but load a configuration to environment. My application context the test:

<bean class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close" id="appDataSource">
    <property name="driverClassName" value="${database.driverClassName}" />
    <property name="url" value="${database.url}" />
    <property name="username" value="${database.username}" />
    <property name="password" value="${database.password}" />
</bean>

<bean id="jndiClient" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/client"/>
    <property name="jndiEnvironment">
        <props>
            <prop key="properties1">44</prop>
            <prop key="properties2">0</prop>
            <prop key="properties3">XPTO</prop>
        </props>
    </property>
</bean>

I need load this jndiClient. In my class:

    @Component
    public class FactoryLocator  {
         @Qualifier("jndiClient")
         @Autowired
         private JndiObjectFactoryBean jndiClient;

happened and the following error when I run the test:

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.jndi.JndiObjectFactoryBean br.com.simova.bob.factory.FactoryLocator.jndiClient; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiClient' defined in class path resource [test-appContext.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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
    ... 53 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jndiClient' defined in class path resource [test-appContext.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:1486)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    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.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:873)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:815)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
    ... 55 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:1545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1483)
    ... 65 more

Thank for any help!

Sam Brannen

First and foremost, your tests typically should not rely on JNDI being present in a test environment.

The whole point of using JndiObjectFactoryBean is so that you can decouple your application from a direct dependency on JNDI, using dependency injection of components instead.

With that in mind, I would highly recommend that you never inject an instance of JndiObjectFactoryBean into one of your application components. Instead, you should really be injecting the object returned by the JndiObjectFactoryBean into your components. By doing so, you can then configure a mock or stub of your component when writing unit and integration tests, thereby avoiding the need for JNDI for testing.

Now, if for some very good reason you simply must rely directly on JNDI, Spring does provide a mechanism for setting up an in-memory JNDI context. See the Javadoc for SimpleNamingContextBuilder for details.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get jndi environment variables with junit test in Spring?

From Dev

How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

From Dev

How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

From Dev

jndi-name property override - JUnit - Spring - Struts Integration test

From Dev

How to test ConfigurationProperties in Spring with JUnit?

From Dev

How to set up a proper JUnit environment for Spring

From Dev

Persist/commit not working in test environment with Spring JPA JUnit

From Dev

How to run Spring Shell scripts in a JUnit test

From Java

How to make Junit test in spring boot

From Dev

How to write JUnit test with Spring Autowire?

From Dev

How to get JNDI datasource in Spring Boot from IBM WebSphere 9

From Dev

Get JNDI resources programatically in spring

From Dev

Get JNDI resources programatically in spring

From Dev

Spring junit test not working

From Dev

How to get environment variables of remote host

From Dev

How to get to Environment Variables from Run

From Dev

How to get environment variables from minidump?

From Dev

How to get Database connection in Spring using JUnit?

From Dev

How to set rabbitmq connection properties using JNDI variables in Spring xml config?

From Dev

How to set rabbitmq connection properties using JNDI variables in Spring xml config?

From Dev

Docker linking db container with spring boot and get environment variables

From Dev

How to pass environment variables to a sbt test build step in Jenkins?

From Dev

How to pass environment variables to test kitchen in .kitchen.yml

From Dev

How can I test Rust methods that depend on environment variables?

From Dev

How to only autowire a specific class in JUnit spring test?

From Dev

In my JUnit test, how do I verify a Spring RedirectView?

From Dev

How exactly works Spring JUnit test that uses stubs?

From Dev

How to tell spring to only load the needed beans for the JUnit test?

From Dev

How do I JUnit test a Spring autowired constructor?

Related Related

  1. 1

    How to get jndi environment variables with junit test in Spring?

  2. 2

    How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

  3. 3

    How to run JUnit Test in Spring Injecting JBOSS JNDI datasources

  4. 4

    jndi-name property override - JUnit - Spring - Struts Integration test

  5. 5

    How to test ConfigurationProperties in Spring with JUnit?

  6. 6

    How to set up a proper JUnit environment for Spring

  7. 7

    Persist/commit not working in test environment with Spring JPA JUnit

  8. 8

    How to run Spring Shell scripts in a JUnit test

  9. 9

    How to make Junit test in spring boot

  10. 10

    How to write JUnit test with Spring Autowire?

  11. 11

    How to get JNDI datasource in Spring Boot from IBM WebSphere 9

  12. 12

    Get JNDI resources programatically in spring

  13. 13

    Get JNDI resources programatically in spring

  14. 14

    Spring junit test not working

  15. 15

    How to get environment variables of remote host

  16. 16

    How to get to Environment Variables from Run

  17. 17

    How to get environment variables from minidump?

  18. 18

    How to get Database connection in Spring using JUnit?

  19. 19

    How to set rabbitmq connection properties using JNDI variables in Spring xml config?

  20. 20

    How to set rabbitmq connection properties using JNDI variables in Spring xml config?

  21. 21

    Docker linking db container with spring boot and get environment variables

  22. 22

    How to pass environment variables to a sbt test build step in Jenkins?

  23. 23

    How to pass environment variables to test kitchen in .kitchen.yml

  24. 24

    How can I test Rust methods that depend on environment variables?

  25. 25

    How to only autowire a specific class in JUnit spring test?

  26. 26

    In my JUnit test, how do I verify a Spring RedirectView?

  27. 27

    How exactly works Spring JUnit test that uses stubs?

  28. 28

    How to tell spring to only load the needed beans for the JUnit test?

  29. 29

    How do I JUnit test a Spring autowired constructor?

HotTag

Archive