title: How to write unit test case for Global Exception handler using UseExceptionHandler middleware
body: <p>I've created global exception handler class as below</p>
  
  <pre><code>public static void UseGlobalExceptionHandler(this IApplicationBuilder app, ILogger logger)
      {
          app.UseExceptionHandler(
              options =&gt; options.Run(
                  async context =&gt;
                  {
                      var env = context.RequestServices.GetService&lt;IHostingEnvironment&gt;();
                      var general = context.RequestServices.GetService&lt;General&gt;();
                      var exHandlerFeature = context.Features.Get&lt;IExceptionHandlerFeature&gt;();
                      var exception = exHandlerFeature.Error;
                      LogException(logger, exception);
                      JObject response = CreateResponseMessage(exception,env,general);
                      if (!context.Response.HasStarted)
                      {
                          context.Response.Headers.Add("Access-Control-Allow-Credentials", new StringValues("true"));
                          context.Response.Headers.Add("Access-Control-Allow-Origin", new StringValues("*"));
                          context.Response.ContentType = "application/json";
                          context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                          var json = JsonConvert.SerializeObject(response);
                          await context.Response.WriteAsync(json);
                      }         
                  }
                  )
              );
      }
  </code></pre>
  
  <p>I'm new to writing the unit test cases. I want to know how to mock the exception handling middleware with the mentioned parameters/variables in the above mentioned code and write the test case for the same.</p>