使用Spring Cloud Stream Test进行错误测试

时间:2018-04-06 21:52:39

标签: spring spring-cloud spring-test spring-cloud-stream

我们正在使用lua_error管理应用程序之间的邮件。

我们有自定义绑定:

spring-cloud-stream

有些处理器会消耗任务并生成事件:

public interface InboundChannels {

  String TASKS = "domainTasksInboundChannel";
  String EVENTS = "eventsInboundChannel";

  @Input(TASKS)
  SubscribableChannel tasks();

  @Input(EVENTS)
  SubscribableChannel events();

}

public interface OutboundChannels {

  String TASKS = "domainTasksOutboundChannel";
  String EVENTS = "eventsOutboundChannel";

  @Output(TASKS)
  MessageChannel tasks();

  @Output(EVENTS)
  MessageChannel events();

}

现在我们想使用@EnableBinding({InboundChannels.class, OutboundChannels.class}) public class TasksProcessor { public TasksProcessor( UserService userService, @Qualifier(OutboundChannels.EVENTS) MessageChannel eventsChannel ) { this.userService = userService; this.eventsChannel = eventsChannel; } @StreamListener(value = TASKS, condition = "headers['" + TYPE + "']=='" + CREATE_USER + "'") public void createUser(Message<User> message) { final User user = message.getPayload(); userService.save(user) .subscribe(created -> { Message<User> successMessage = fromMessage(message, Events.USER_CREATED, created).build(); eventsChannel.send(successMessage); }); } } 及其惊人的功能来测试它:

spring-cloud-stream-test-support

application.properties

@DirtiesContext
@SpringBootTest
@RunWith(SpringRunner.class)
public class TasksProcessorTest {

  private User user;

  @Autowired
  private InboundChannels inboundChannels;

  @Autowired
  private OutboundChannels outboundChannels;

  @Autowired
  private MessageCollector collector;

  @Before
  public void setup() {
    user = new User(BigInteger.ONE, "test@teste.com");
  }

  @Test
  public void createUserTest() {
    final Message<User> msg = create(CREATE_USER, user).build();
    outboundChannels.tasks().send(msg);
    final Message<?> incomingEvent = collector.forChannel(inboundChannels.events()).poll();
    final String type = (String) incomingEvent.getHeaders().get(TYPE);
    assertThat(type).isEqualToIgnoringCase(USER_CREATED);
  }

}

spring.cloud.stream.bindings.domainTasksInboundChannel.destination = domainTasks

spring.cloud.stream.bindings.userTasksInboundChannel.group = domainServiceInstances spring.cloud.stream.bindings.eventsInboundChannel.group = domainServiceInstances

但是我们得到了这个错误:

##
# Spring AMQP configuration
##
spring.rabbitmq.host=rabbitmq
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

# Events channels
spring.cloud.stream.bindings.eventsOutboundChannel.destination=events
spring.cloud.stream.bindings.eventsInboundChannel.destination=events

spring.cloud.stream.bindings.domainTasksOutboundChannel.destination=domainTasks

我们做错了什么?

1 个答案:

答案 0 :(得分:0)

.subscribe() eventsChannel.send(successMessage); eventsChannel中,OutboundChannels.EVENTS来自inboundChannels.events(),但您在测试用例中尝试做的事情就像{{1} }}。并且看起来你真的将这个频道绑定在任何地方。

我确定您是否会使用outboundChannels.events()代替,这对您有用。