Spring Cloud Stream如何测试自定义源和输出

时间:2018-10-16 11:49:40

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

Spring云流如何测试自定义Source和Output?

public interface DeviceManagerOutput {
   String OUTPUT = "deviceManager";

   @Output( DeviceManagerOutput.OUTPUT )
   MessageChannel output();
}

@Service
@EnableBinding( DeviceManagerOutput.class )
@RequiredArgsConstructor
public class DeviceManagerSink {
   private final DeviceManagerOutput source;

   @ServiceActivator( outputChannel = DeviceManagerOutput.OUTPUT )
   @LogName
   public void sendDeviceManagerUpdate( String message ) {
      source.output().send( new GenericMessage<>( message ) )
   }

}

MessageSender.java

@Slf4j
@Service
@RequiredArgsConstructor
public class MessageSender {
  private final DeviceManagerSink deviceManagerSink;

  public void sendMessage(String message) {
    route(message);
  }

  @Override
  @LogName
  public void route( String message ) {
      log.info( "Ingoring the Source provide.. using another source for configuration message.." );
      this.deviceManagerSink.sendDeviceManagerUpdate( message );
   }
}

如何测试对MessageChannel发送方法的调用是否成功?

测试类

@RunWith( MockitoJUnitRunner.class )
public class MessageSenderTest {

    @Mock
    private MessageSender messageSender;

    @Mock
    private DeviceManagerSink deviceManagerSink;

    @Test
    public void test_route_message() {
        this.messageSender.sendMessage("Hi");
        verify( deviceManagerSink, times( 1 ) ).sendDeviceManagerUpdate( any() ); // gives success    
}

在sendDeviceManagerUpdate内部,我们正在调用DeviceManagerOutput的send方法。如何测试send方法的调用是否成功?

0 个答案:

没有答案