Artemis activeMQ embedded 不支持 spring 集成测试

Artemis activeMQ embeded not working with spring integration tests

我尝试在 Spring 启动集成测试中使用嵌入模式的 artemis activeMQ,但是当我发送消息时,它无法接收。 我使用本机模式,它运行良好,但当我 运行 集成测试(嵌入式)时情况并非如此。

这是我的 applications-test.properties (src/test/resources) :

spring.artemis.mode=embedded
spring.artemis.host=localhost
spring.artemis.port=61616
spring.artemis.user=exampleuser
spring.artemis.password=examplepassword
spring.artemis.embedded.topics=exampleTopic,exampleTopic2
jms.queue.destination=exampleQueue
spring.jms.pub-sub-domain=true

我的 broker.xml.bak (src/test/resources) :

<?xml version='1.0'?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:activemq" xsi:schemaLocation="urn:activemq /schema/artemis-server.xsd">
<jms xmlns="urn:activemq:jms">
    <!--the queue used by the example -->
    <queue name="exampleQueue" />
</jms>

<core xmlns="urn:activemq:core">

    <persistence-enabled>false</persistence-enabled>
    <journal-type>NIO</journal-type>

    <!-- Acceptors -->
    <acceptors>
        <acceptor name="netty">tcp://localhost:61616</acceptor>
    </acceptors>

    <!-- Other config -->

    <security-settings>
        <!--security for example queue -->
        <security-setting match="#">
            <permission type="createDurableQueue" roles="example" />
            <permission type="deleteDurableQueue" roles="example" />
            <permission type="createNonDurableQueue" roles="example" />
            <permission type="deleteNonDurableQueue" roles="example" />
            <permission type="consume" roles="example" />
            <permission type="send" roles="example" />
        </security-setting>
    </security-settings>

</core>
</configuration>

生产者jms消息Class:

@Component
public class JmsProducer {

  @Autowired
  private
  JmsTemplate jmsTemplate;

  @Value("${jms.queue.destination}")
  private String destinationQueue;

  public void send(String msg){
      getJmsTemplate().convertAndSend(destinationQueue, msg);
  }

  public JmsTemplate getJmsTemplate() {
      return jmsTemplate;
  }
 }

这是集成测试 Class :

@RunWith(SpringRunner.class)
@SpringBootTest
@TestPropertySource(locations = {"/application-test.properties","/broker.xml.bak"})
public class outilConverterApplicationTests {

@Autowired
private JmsProducer producer;

@Autowired
private ArtemisProperties artemisProperties;

@Before
public void init(){
    customizer();
}

public ArtemisConfigurationCustomizer customizer() {
    return new ArtemisConfigurationCustomizer() {

        @Override
        public void customize(org.apache.activemq.artemis.core.config.Configuration configuration) {
            try {
                configuration.addAcceptorConfiguration("netty", "tcp://localhost:" + artemisProperties.getPort());

            } catch (Exception e) {
                throw new RuntimeException("Failed to add netty transport acceptor to artemis instance", e);
            }

        }

    };
}

@Test
public void send(){
    producer.send("MyMessage");
    producer.getJmsTemplate().setReceiveTimeout(20_000);
    assertThat(producer.getJmsTemplate().receiveAndConvert("exampleQueue")).isEqualTo("MyMessage");
}
}

Junit Contole 跟踪错误:

org.junit.ComparisonFailure: expected:<"MyMessage"> but was:<null>

我将 DefaultDestinationName 设置为 JmsTemplate:

jmsTemplate.setDefaultDestinationName("exampleQueue");

我也在应用程序中更改-test.properties:

spring.jms.pub-sub-domain=false

终于一切顺利。