我正在尝试运行在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配置文件中初始化
解决方法:
将@ActiveProfiles(“test”)添加到您的测试类
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = { App.class }, webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration
@TestPropertySource("classpath:/application-test.properties")
@ActiveProfiles("test")
public class CheckingServiceTest {