2016-12-22 1 views
0

테스트에서 응용 프로그램을 시작한 다음 청취자 봄 부팅 응용 프로그램에 대한 통합 테스트를 작성하려고합니다. 그런 다음 메시지를 생성하고 출력 채널에 드롭 한 다음 메시지를 선택하여 처리 할 때까지 기다립니다. 여기 @StreamListener와 RabbitMQ를 어떻게 (통합하여) 테스트합니까?

내가 시험이 무엇을 : 더를 거기로

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 

그러나 입력 교환에 바인딩 한이 시험은 실패하지 않습니다 메시지가 수신되었습니다. 나는 무엇을 찾고 있어야 하는가?

편집 - 완전성에 대한 리스너 코드

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'가 있음)를 붙여 넣지 않았습니다. 실제로 작동하는지 확인했습니다. 인바운드 채널의 경우 기본값은 '입력'이라고 생각합니다. – Raghu

관련 문제