Spring Boot 2.0 JNDI属性值未从应用程序测试属性文件加载

时间:2018-10-11 13:22:28

标签: java spring spring-boot jndi spring-boot-test

我正在尝试运行在application-test.properties文件中配置的测试用例和jndi属性

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

属性文件中的配置

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

在Xml配置文件中

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

在运行测试用例时,它会抛出

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

直接尝试将静态值放入bean初始化中,它可以正常工作,甚至使用application.properties中提到的默认配置,应用程序也可以正常工作。

问题:属性值未在XML配置文件中初始化

3 个答案:

答案 0 :(得分:1)

尝试将您的TestPropertySource批注更改为:

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

答案 1 :(得分:1)

@ActiveProfiles("test")添加到测试班

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

答案 2 :(得分:0)

从Project中删除XML配置,并尝试创建每个驱动的bean注释。

使用@Value为每个变量分配值,或使用

创建单独的类
 @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);
}

}