title: Unit test for Server sent event client, Spring WebClient
body: <p>I need to write unit test for this method:</p>
  <pre><code>import org.springframework.web.reactive.function.client.WebClient;
  import reactor.core.publisher.Flux;
   
   @Service
  public class ConsumeDataServiceImpl implements ConsumeDataService {
  public void consumeServerSentEvent(String baseUrl) {
          WebClient client = WebClient.create(baseUrl);
          ParameterizedTypeReference&lt;ServerSentEvent&lt;String&gt;&gt; type
                  = new ParameterizedTypeReference&lt;ServerSentEvent&lt;String&gt;&gt;() {};
  
          Flux&lt;ServerSentEvent&lt;String&gt;&gt; eventStream = client.get()
                  .uri(&quot;/&quot;)
                  .retrieve()
                  .bodyToFlux(type);
  
          eventStream.subscribe(
                  content -&gt; System.out.println(&quot;***received***&quot;),
                  error -&gt; System.out.println(&quot;***error*** &quot; + error),
                  () -&gt; System.out.println(&quot;***completed***&quot;));
      }
  }
  </code></pre>
  <p>My unit test is like this:</p>
  <pre><code> @Test
      public void testConsumeServerSentEvent()  {
          MockWebServer mockBackEnd = new MockWebServer();
          try {
              mockBackEnd.start();
          } catch (IOException e) {
              System.out.println(&quot;error in start mockbackend&quot;);
          }
          mockBackEnd.enqueue(
                  new MockResponse()
                          .setResponseCode(200)
                          //.setHeader(&quot;Accept&quot;, &quot;text/event-stream&quot;)
                          .setBody(Flux.just(&quot;Spring MVC&quot;,&quot;Spring Boot&quot;,&quot;Spring Web&quot;).toString())
          );
          Environment env = Mockito.mock(Environment.class);
          MyRepository repo = Mockito.mock(MyRepository.class);
          ConsumeDataServiceImpl consumeService = new ConsumeDataServiceImpl(env,repo);
          consumeService.consumeServerSentEvent(mockBackEnd.url(&quot;/&quot;).toString());
      }
  </code></pre>
  <p>But when i run unit test then in eventStream.subscribe in the method i receive error: Java.lang.IllegalStateException: executor not accepting a task</p>