4

I am trying to run test cases and jndi properties configured in application-test.properties file

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { App.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@TestPropertySource("classpath:/application-test.properties")
public class CheckingServiceTest {

A configuration in the property file

java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory
java.naming.provider.url=remote://xyz:4447,remote://xyz:4447
java.naming.security.principal=qaappmq
java.naming.security.credentials=xyz123
jms.username=qaappmq
jms.password=ixyz123
jboss.naming.client.ejb.context=true

In Xml configuration file

<bean id="JNDITemplate" class="org.springframework.jndi.JndiTemplate">
        <constructor-arg name="environment">
            <props>
                <prop key="java.naming.factory.initial">${java.naming.factory.initial}</prop>
                <prop key="java.naming.provider.url">${java.naming.provider.url}</prop>
                <prop key="java.naming.security.principal">${java.naming.security.principal}</prop>
                <prop key="java.naming.security.credentials">${java.naming.security.credentials}</prop>
                <prop key="jboss.naming.client.ejb.context">${jboss.naming.client.ejb.context}</prop>
            </props>
        </constructor-arg>
    </bean>

While running the test case it is throwing

Caused by: java.lang.ClassNotFoundException: ${java.naming.factory.initial}
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:72)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:61)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:672)
    ... 92 common frames omitted

Tried directly putting static value in bean intialization it is working fine and even application is working fine with default configuration mentioned in application.properties.

Issue : Properties value is not intialized in XML configuration file

Kumar Panchal
  • 186
  • 13
  • What exactly are you intending to do ? If you want to use same main application properties in test you don't need any changes (as you've observed in the post). What is the use of xml ? Are you trying to add/override properties just for test ? – s7vr Oct 22 '18 at 17:06
  • I want to initialize property value in xml file from property file – Kumar Panchal Oct 23 '18 at 05:02
  • You already have a property file. Why initialize and use xml ? If you need test properties use application-test property. Sorry I don't understand your requirement. – s7vr Oct 23 '18 at 08:06
  • I am using application-test.properties file. actually, issue with only this JNDI property initialization and other properties are loading fine. – Kumar Panchal Oct 23 '18 at 09:18
  • can you please give me example – Kumar Panchal Oct 23 '18 at 10:07
  • other all properties are initializing successfully in both cases while running and testing. but while testing only this properties not initializing specific to only JNDI property – Kumar Panchal Oct 23 '18 at 10:30
  • dear i need value of property key from property value – Kumar Panchal Oct 23 '18 at 10:35
  • Can you add the application-test.properties and App class to the post ? – s7vr Oct 23 '18 at 10:47
  • The @TestPropertySource annotation tries to load a properties file relative to the class that declared the annotation. Try to move your test properties to the package of the test class and remove the "classpath:/" form the annotation value. BTW: Using the annotation in this way you're able to define the properties in the test, if you want/need it: @TestPropertySource(properties= {"foo.bar=baz","java.naming.security.credentials=xyz123"}) – ibexit Oct 25 '18 at 21:47
  • Did you find a solution to the problem? – mihomir Mar 21 '19 at 12:02
  • Yeah, Removed XML configuration from my project and created every required bean using annotation. – Kumar Panchal Mar 25 '19 at 08:18

3 Answers3

1

Try changing your TestPropertySource annotation to:

@TestPropertySource(locations = "classpath:application-test.properties")

Fabio Manzano
  • 2,847
  • 1
  • 11
  • 23
  • 1
    Already mentioned this code and this annotation will use to initialize values which are annotated with @Value annotation – Kumar Panchal Oct 29 '18 at 05:02
1

Add @ActiveProfiles("test") to your test class

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { App.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@TestPropertySource("classpath:/application-test.properties")
@ActiveProfiles("test")
public class CheckingServiceTest {
Federico Piazza
  • 30,085
  • 15
  • 87
  • 123
0

Remove XML Configuration from Project and Try to create each bean annotation driven.

Assigned value each varible using @Value or create seperate class using

 @Configuration
 public class TestConfiguration {

@Bean(name = "publishJNDITemplate")
public JndiTemplate publishJNDITemplate() {
    final Properties environment = new Properties();
    environment.put(Context.INITIAL_CONTEXT_FACTORY, factoryIntial);
    environment.put(Context.PROVIDER_URL, providerUrl);
    environment.put(Context.SECURITY_PRINCIPAL, securityPrincipal);
    environment.put(Context.SECURITY_CREDENTIALS, securityCredentials);
    environment.put(narfeedTopics, destination);
    return new JndiTemplate(environment);
}

}

Kumar Panchal
  • 186
  • 13