Artemis activeMQ嵌入了弹簧集成测试

时间:2018-04-26 14:54:24

标签: spring spring-boot activemq broker activemq-artemis

我尝试在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消息类:

@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;
  }
 }

以下是集成测试类:

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

1 个答案:

答案 0 :(得分:0)

我将DefaultDestinationName设置为JmsTemplate:

jmsTemplate.setDefaultDestinationName("exampleQueue");

我也改变了application-test.properties:

spring.jms.pub-sub-domain=false

最后,每件事情都很好。