2017-03-15 34 views
0

我想要寫一個類似下面的測試;寫作測試,以驗證收到味精JMS監聽器(彈簧引導)

  1. 有一個在src/main稱爲state-info-1聽衆。

  2. 它做一些改變,它得到的任何消息和ActiveMQ的話題state-info-2發佈新的消息。

  3. 我將建立一個假消息,併發布到ActiveMQ的話題state-info-1

  4. 最後確認,在題目state-info-2收到的消息是喜歡我的預期。

我的聽衆很喜歡;

@JmsListener(destination = "state-info-1", containerFactory = "connFactory") 
public void receiveMessage(Message payload) { 
    // Do Stuff and Publish to state-info-2 
} 

是否有可能我可以爲此編寫測試?或者我必須以其他方式做到這一點?

而且,我看着這樣的:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-samples/spring-boot-sample-activemq/src/test/java/sample/activemq/SampleActiveMqTests.java

但是,這不是我所期待的。

在正確的方向任何幫助或推就足夠了。

謝謝你的時間。

回答

3
@SpringBootApplication 
public class So42803627Application { 

    public static void main(String[] args) { 
     SpringApplication.run(So42803627Application.class, args); 
    } 

    @Autowired 
    private JmsTemplate jmsTemplate; 

    @JmsListener(destination = "foo") 
    public void handle(String in) { 
     this.jmsTemplate.convertAndSend("bar", in.toUpperCase()); 
    } 

} 

@RunWith(SpringRunner.class) 
@SpringBootTest 
public class So42803627ApplicationTests { 

    @Autowired 
    private JmsTemplate jmsTemplate; 

    @Test 
    public void test() { 
     this.jmsTemplate.convertAndSend("foo", "Hello, world!"); 
     this.jmsTemplate.setReceiveTimeout(10_000); 
     assertThat(this.jmsTemplate.receiveAndConvert("bar")).isEqualTo("HELLO, WORLD!"); 
    } 

} 
+0

感謝。雖然這適用於'Queues',不適用於我使用的activemq'Topics'。當我有屬性'spring.jms.pub-sub-domain = true'時不起作用。獲取'null'。 – Raj

+2

這只是JMS主題的工作方式 - 默認情況下,訂閱不是持久的,只有那些在發佈消息時處於活動狀態的消費者纔會收到消息。您可能需要等待聽衆發送前訂閱,或使認購耐用(這意味着你只需要等待你第一次運行測試)。 –

+0

明白了!讓它等待,它工作正常。再次感謝 :) – Raj