Java 如何进行单元测试并向嵌入式代理发送消息?

Java 如何进行单元测试并向嵌入式代理发送消息?,java,unit-testing,junit,jms,activemq-artemis,Java,Unit Testing,Junit,Jms,Activemq Artemis,我需要运行一个嵌入式ActiveMQ Artemis 有此代码: ConnectionFactory ConnectionFactory=new-ActiveMQConnectionFactory(“vm://localhost?broker.persistent=false”); 会话会话=connectionFactory.createConnection().createSession(); MessageProducer=session.createProducer(空); JmsReq

我需要运行一个嵌入式ActiveMQ Artemis

有此代码:

ConnectionFactory ConnectionFactory=new-ActiveMQConnectionFactory(“vm://localhost?broker.persistent=false”);
会话会话=connectionFactory.createConnection().createSession();
MessageProducer=session.createProducer(空);
JmsRequest JmsRequest=newJMSRequest(“jsonMsg”,JmsRequestStatus.new,“targetQueueTESTTEST”);
Topic Topic=session.createTopic(jmsRequest.getTargetQueue());
TextMessage=session.createTextMessage(jmsRequest.getJsonMsg());
producer.send(主题、消息);
返回此异常:

javax.jms.JMSException: Failed to create session factory    
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:886)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294)
    at com.americanwell.caretalks.jms.entities.PracticeITTest.aaa(PracticeITTest.java:17)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.IllegalStateException: java.lang.ClassNotFoundException: org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory
    at org.apache.activemq.artemis.utils.ClassloadingUtil.newInstanceFromClassLoader(ClassloadingUtil.java:59)
    at org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl$2.run(ClientSessionFactoryImpl.java:1002)
    at org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl$2.run(ClientSessionFactoryImpl.java:999)
    at java.security.AccessController.doPrivileged(Native Method)
    at org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl.instantiateConnectorFactory(ClientSessionFactoryImpl.java:999)
    at org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl.<init>(ClientSessionFactoryImpl.java:181)
    at org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:781)
    at org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:884)
javax.jms.JMSException:无法创建会话工厂
位于org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:886)
位于org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:299)
位于org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:294)
位于com.americanwell.caretalks.jms.entities.practiceitest.aaa(practiceitest.java:17)
在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处
位于sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)中
位于java.lang.reflect.Method.invoke(Method.java:498)
位于org.junit.runners.model.FrameworkMethod$1.runReflectVeCall(FrameworkMethod.java:47)
位于org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
位于org.junit.runners.model.FrameworkMethod.invokeeexplosive(FrameworkMethod.java:44)
位于org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
位于org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
位于org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
位于org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
位于org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
位于org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
访问org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
位于org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
位于org.junit.runners.ParentRunner.run(ParentRunner.java:309)
位于org.junit.runner.JUnitCore.run(JUnitCore.java:160)
位于com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
位于com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
位于com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
位于com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
原因:java.lang.IllegalStateException:java.lang.ClassNotFoundException:org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory
位于org.apache.activemq.artemis.utils.ClassloadingUtil.newInstanceFromClassLoader(ClassloadingUtil.java:59)
位于org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl$2.run(ClientSessionFactoryImpl.java:1002)
位于org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl$2.run(ClientSessionFactoryImpl.java:999)
位于java.security.AccessController.doPrivileged(本机方法)
位于org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl.InstanceConnectorFactory(ClientSessionFactoryImpl.java:999)
位于org.apache.activemq.artemis.core.client.impl.ClientSessionFactoryImpl.(ClientSessionFactoryImpl.java:181)
位于org.apache.activemq.artemis.core.client.impl.ServerLocatorImpl.createSessionFactory(ServerLocatorImpl.java:781)
位于org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory.createConnectionInternal(ActiveMQConnectionFactory.java:884)

我如何解决这个问题?如何从嵌入式代理获取会话?

这里的主要问题是:

java.lang.ClassNotFoundException: org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory
您需要将
org.apache.activemq.artemis.core.remoting.impl.invm.InVMConnectorFactory
放在类路径上,以便应用程序可以使用它。此类在
artemis服务器
Maven模块中,例如:


org.apache.activemq
artemis服务器
2.13.0

如果您要手动构建类路径,那么只需包含
artemis服务器-.jar
。它在
lib
目录下的代理发行版中发布。

我的回答是否回应了您的评论?如果是,请将其标记为正确,以帮助将来有相同问题的其他用户。如果没有,请详细说明缺少什么。谢谢