title: How to test function returning Mono<Void> which has another Mono<T> within using Reactor's StepVerifier
body: <p>I have a ServiceWebClientInterface.java like this</p>
  <pre><code>import reactor.core.publisher.Mono;
  
  public interface ServiceWebClientInterface {
  
      Mono&lt;String&gt; apiCall();
  }
  </code></pre>
  <p>MyClass.java</p>
  <pre><code>import org.springframework.cloud.gateway.filter.GatewayFilter;
  import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
  import org.springframework.http.HttpStatus;
  
  public class MyClass extends AbstractGatewayFilterFactory&lt;MyClass.Config&gt; {
  
      private final ServiceWebClientInterface serviceWebClientInterface;
  
      MyClass(final ServiceWebClientInterface serviceWebClientInterface) {
          this.serviceWebClientInterface = serviceWebClientInterface;
      }
  
      @Override
      public GatewayFilter apply(Config config) {
          return (exchange, chain) -&gt; {
              return serviceWebClientInterface.apiCall().flatMap(response -&gt; {
                  if (!&quot;Valid&quot;.equals(response)) {
                      exchange.getResponse().setStatusCode(HttpStatus.FORBIDDEN);
                      return exchange.getResponse().setComplete();
                  }
                  return chain.filter(exchange);
              });
  
          };
      }
  
      public static class Config  {
          // Put the configuration properties
      }
  }
  </code></pre>
  <p>I'm trying to unit test myMethod using StepVerifier, but I am not able to execute statements inside the inner lambda function of myMethod.</p>
  <p>MyClassTest.java</p>
  <pre><code>import org.junit.jupiter.api.BeforeEach;
  import org.junit.jupiter.api.Test;
  import org.mockito.Mock;
  import org.mockito.MockitoAnnotations;
  import org.springframework.cloud.gateway.filter.GatewayFilter;
  import org.springframework.cloud.gateway.filter.GatewayFilterChain;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.server.reactive.ServerHttpResponse;
  import org.springframework.web.server.ServerWebExchange;
  import reactor.core.publisher.Mono;
  import reactor.test.StepVerifier;
  
  import static org.mockito.ArgumentMatchers.eq;
  import static org.mockito.Mockito.*;
  class MyClassTest {
  
      @Mock
      ServiceWebClientInterface mockServiceWebClientInterface;
      @Mock
      private ServerWebExchange mockServerWebExchange;
      @Mock
      private GatewayFilterChain mockGatewayFilterChain;
      @Mock
      private ServerHttpResponse mockServerHttpResponse;
  
      @BeforeEach
      void setup() {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      void test_apply_forValid() {
          when(mockServiceWebClientInterface.apiCall()).thenReturn(Mono.just(&quot;Valid&quot;));
          MyClass.Config config = new MyClass.Config();
          MyClass myClass = new MyClass(mockServiceWebClientInterface);
  
          GatewayFilter gatewayFilter = myClass.apply(config);
  
          Mono&lt;Void&gt; response = gatewayFilter.filter(mockServerWebExchange, mockGatewayFilterChain);
  
          StepVerifier.create(response).expectComplete();
  
          verify(mockServiceWebClientInterface).apiCall();
          verify(mockGatewayFilterChain).filter(mockServerWebExchange);
  
      }
  
      @Test
      void test_apply_forInValid() {
          when(mockServiceWebClientInterface.apiCall()).thenReturn(Mono.just(&quot;InValid&quot;));
          when(mockServerWebExchange.getResponse()).thenReturn(mockServerHttpResponse);
          MyClass.Config config = new MyClass.Config();
          MyClass myClass = new MyClass(mockServiceWebClientInterface);
  
          GatewayFilter gatewayFilter = myClass.apply(config);
  
          Mono&lt;Void&gt; response = gatewayFilter.filter(mockServerWebExchange, mockGatewayFilterChain);
  
          StepVerifier.create(response).expectComplete();
  
          verify(mockServiceWebClientInterface).apiCall();
          verify(mockServerHttpResponse).setStatusCode(eq(HttpStatus.FORBIDDEN));
          verify(mockServerHttpResponse).setComplete();
          verify(mockGatewayFilterChain, never()).filter(mockServerWebExchange);
  
      }
  
  
  }
  </code></pre>
  <p>Please find the complete code above, When I run the tests I observe that the inner lambda function does not get invoked using the step verifier.</p>