2016-12-22 6 views
0

テストでアプリケーションを起動し、メッセージを作成し、出力チャンネルにドロップし、メッセージが選択されて処理されるのを待つリスナー春起動アプリケーションの統合テストを作成しようとしています。RabbitMQで@StreamListenerを(統合して)テストするにはどうすればよいですか?

は、ここで私はテストのために持っているものだ:

package com.example; 

import com.netflix.discovery.converters.Auto; 
import org.junit.Before; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.test.context.SpringBootTest; 
import org.springframework.cloud.stream.annotation.EnableBinding; 
import org.springframework.cloud.stream.messaging.Source; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.messaging.Message; 
import org.springframework.messaging.support.MessageBuilder; 
import org.springframework.test.context.ActiveProfiles; 
import org.springframework.test.context.junit4.SpringRunner; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.test.web.servlet.setup.MockMvcBuilders; 
import org.springframework.web.context.WebApplicationContext; 

import static org.assertj.core.api.Assertions.assertThat; 
import static org.springframework.integration.support.management.graph.LinkNode.Type.output; 

@RunWith(SpringRunner.class) 
@SpringBootTest 
@ActiveProfiles("scratch") 
public class DemoApplicationTests { 

    @Component 
    @EnableBinding(Source.class) 
    static class TestMessageSource { 

     @Autowired 
     private Source source; 

     public void sendMessage(String message) { 
      new Thread(() -> { 
       Message<Greeting> msg = MessageBuilder.withPayload(new Greeting(message)).build(); 
       source.output().send(msg); 
      }).start(); 
     } 
    } 

    static class CounterFakeService implements CounterService { 
     public int count = 0; 
     public Greeting greeting; 
     @Override 
     public void recordCount(Greeting greeting) { 
      count++; 
      this.greeting = greeting; 
     } 
    } 

    private CounterFakeService fakeCounterService; 
    @Bean 
    CounterService counterService() { 
     return fakeCounterService; 
    } 

    @Autowired 
    TestMessageSource messageSource; 

    @Before 
    public void before() { 
     fakeCounterService = new CounterFakeService(); 
//  this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build(); 
    } 

    @Test 
    public void doesProcessMessages() throws InterruptedException { 
     assertThat(fakeCounterService.count).isEqualTo(0); 
     messageSource.sendMessage("test"); 
     Thread.sleep(5000); 
     assertThat(fakeCounterService.count).isEqualTo(1); 
     assertThat(fakeCounterService.greeting.getMessage()).isEqualTo("test"); 
    } 

} 

application-scratch.propertiesで私が入力交換に

spring.rabbitmq.host=rabbitmq.local.pcfdev.io 
spring.rabbitmq.port=5672 
spring.rabbitmq.password=i9jbk2o3ingqtkrekgm988bvui 
spring.rabbitmq.username=8cf073b0-a2ff-450a-bee5-3954cb6c191f 
spring.rabbitmq.virtual-host=5fc33451-4ec0-440e-90a1-6e7ed0c025f9 
spring.cloud.stream.bindings.output.destination=input 

をバインド出力チャンネルはしかし、現時点では、テストにはありますとして失敗していませんメッセージが受信されました。私は何を探していますか?

EDIT - 完全性についてリスナーコード

PS:これは実際に動作するようにテストを取得することはできませんinput exchange..Justにメッセージを送信する別のアプリで動作することを私が確認した:(

@RestController 
@SpringBootApplication 
@EnableDiscoveryClient 
@EnableBinding(Sink.class) 
public class DemoApplication { 
    private static Logger log = LoggerFactory.getLogger(DemoApplication.class); 

    @Autowired 
    private Config config; 

    @Autowired 
    CounterServiceImpl counterService; 

    @StreamListener(Sink.INPUT) 
    public void handle(Greeting greeting) { 
     log.info("in handle(Greeting), {}", greeting); 
     counterService.recordCount(greeting); 
    } 

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

答えて

0

Facepalm time! :) 私はバックグラウンドで実際のリスナーを実行していましたが、テストが通過しなかったためにすべてのメッセージを受け取っていました

0

私は、受信チャネルとのCounterServiceのための任意の@StreamListenerの設定が表示されません。あなたはまた、同じ宛先inputへのインバウンドチャネル用のバインディング設定を必要としています。

+0

実際のコード( '@ StreamListener'を持っています)私はそれが実際に動作することを確認しました。インバウンドチャンネルについては、デフォルトは 'input'だと思います。 – Raghu

関連する問題