title: While Unit Testing a .Net core 3.1 Background Service, unable to resolve IConfiguration
body: <p>I'm trying to write unit test case for the BackgroundService <code>Worker.cs</code>. I have read <a href="https://stackoverflow.com/questions/51224146/integration-test-for-hosted-service-net-core">Stack Overflow Question</a></p>
  
  <p>But still I get the error that </p>
  
  <blockquote>
    <p>"Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfiguration' while attempting to activate 'AutoClueArchiver.Worker'.</p>
  </blockquote>
  
  <pre><code>public WorkerTests()
  {
      _config = new ConfigurationBuilder()
              .AddJsonFile("appsettings.json", true, true)
              .AddJsonFile("appsettings.Development.json", true, true)
              .AddJsonFile("appsettings.local.json", true, true)
              .AddJsonFile("\\\\charon.cmiprog.com\\devinet\\Configuration\\" + "ApiEndpoints.json", false, true)
              .AddJsonFile("ApiEndpoints.local.json", true, true)
              .AddJsonFile("\\\\charon.cmiprog.com\\devinet\\Configuration\\" + "Kafka.json", false, true)
              .AddEnvironmentVariables().Build();
           _mockedKafkaTopicConsumerManager =new Mock&lt;IKafkaTopicConsumerManager&gt;();
           _mockedMessageProcessor=new Mock&lt;IMessageProcessingCapable&gt;();
  }
  
  [Fact]
  public async Task ExecuteAsync_Test()
  {            
       IServiceCollection services=new ServiceCollection();
       services.AddSingleton&lt;IConfigEntriesClientService, ConfigEntriesClientServiceInjectable&gt;();
       services.AddSingleton(typeof(IProducer), s =&gt; new KafkaProducer(s.GetRequiredService&lt;IProducer&lt;string, string&gt;&gt;(), s.GetRequiredService&lt;IConfiguration&gt;().GetValue&lt;string&gt;("Shared:Kafka:TopicSuffix")));
       services.AddSingleton(typeof(IProducer&lt;string, string&gt;), c =&gt; new ProducerBuilder&lt;string, string&gt;(KafkaConfigs(c)).Build());
       services.AddHttpClient();
       services.AddScoped&lt;IPolicyApiClient, PolicyApiClient&gt;();
       services.AddTransient&lt;IFilterMessages, MessageFilter&gt;();
       services.AddTransient&lt;IArchiveAutoClues, Archiver&gt;();
       services.AddTransient&lt;IFileSystem, FileSystem&gt;();
       services.AddTransient&lt;ISaveDocument, DocumentManager&gt;();
       services.AddTransient&lt;IKafkaTopicConsumerManager, KafkaTopicConsumerManager&gt;();
       services.AddTransient&lt;IMessageProcessingCapable, ConsumerInitializer&gt;();
       services.AddTransient&lt;Consumer&gt;();
       services.AddTransient&lt;IExceptionPublisher, ExceptionPublisher&gt;();
       services.AddTransient&lt;IHttpContextAccessor, HttpContextAccessor&gt;();
       services.AddScoped(sp =&gt; new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate });
       services.AddHttpClient&lt;IBaseApiClient, BaseApiClient&gt;().ConfigurePrimaryHttpMessageHandler(
       sp =&gt; new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate }
              );
  
       var worker=services.AddHostedService&lt;Worker&gt;();
       var serviceProvider=services.BuildServiceProvider();
       var backgroundService = serviceProvider.GetService&lt;IHostedService&gt;() as Worker;
       await backgroundService?.StartAsync(CancellationToken.None);
       await Task.Delay(1000);
       await backgroundService?.StopAsync(CancellationToken.None);
       //await backgroundService.ExecuteAsync(new CancellationToken()); 
       //Any way to access ExecuteAsync here since I get protection level error as 
       //ExecuteAsync is protected
       _mockedKafkaTopicConsumerManager.Verify(c=&gt;c.StartConsumption
       (It.IsAny&lt;CancellationToken&gt;(), 
       _mockedMessageProcessor.Object,
                  It.IsAny&lt;string&gt;(),
                  It.IsAny&lt;string&gt;(),
                  It.IsAny&lt;List&lt;string&gt;&gt;(),
                  It.IsAny&lt;string&gt;(),
                  It.IsAny&lt;int&gt;()),Times.Once);
  }
  </code></pre>