title: Net Core: Execute All Dependency Injection in Xunit Test for AppService, Repository, etc
body: <p>I am trying to implement Dependency Injection in Xunit test for AppService.  Ideal goal is to run the original application program Startup/configuration, and use any dependency injection that was in Startup, instead of reinitializing all the DI again in my test, thats the whole Goal in question.</p>
  
  <p>Update: Mohsen's answer is close. Need to update couple syntax/requirement errors to work.</p>
  
  <p>For some reason, original application works and can call Department App Service. However, it cannot call in Xunit. Finally got Testserver working using Startup and Configuration from original application. 
  Now receiving error below: </p>
  
  <pre><code>Message: The following constructor parameters did not have matching fixture data: IDepartmentAppService departmentAppService
  
  namespace Testing.IntegrationTests
  {
      public class DepartmentAppServiceTest
      {
          public DBContext context;
          public IDepartmentAppService departmentAppService;
  
          public DepartmentAppServiceTest(IDepartmentAppService departmentAppService)
          {
              this.departmentAppService = departmentAppService;
          }
  
          [Fact]
          public async Task Get_DepartmentById_Are_Equal()
          {
              var options = new DbContextOptionsBuilder&lt;SharedServicesContext&gt;()
                  .UseInMemoryDatabase(databaseName: "TestDatabase")
                  .Options;
              context = new DBContext(options);
  
              TestServer _server = new TestServer(new WebHostBuilder()
                  .UseContentRoot("C:\\OriginalApplication")
                  .UseEnvironment("Development")
                  .UseConfiguration(new ConfigurationBuilder()
                      .SetBasePath("C:\\OriginalApplication")
                      .AddJsonFile("appsettings.json")
                      .Build()).UseStartup&lt;Startup&gt;());
  
              context.Department.Add(new Department { DepartmentId = 2, DepartmentCode = "123", DepartmentName = "ABC" });
              context.SaveChanges();
  
              var departmentDto = await departmentAppService.GetDepartmentById(2);
  
              Assert.Equal("123", departmentDto.DepartmentCode);
          }
      }
  }
  </code></pre>
  
  <p>I am receiving this error:</p>
  
  <pre><code>Message: The following constructor parameters did not have matching fixture data: IDepartmentAppService departmentAppService
  </code></pre>
  
  <p>Need to use Dependency injection in testing just like real application.
  Original application does this. Answers below are not currently sufficient , one uses mocking which is not current goal, other answer uses Controller which bypass question purpose.</p>
  
  <p>Note: IDepartmentAppService has dependency on IDepartmentRepository which is also injected in Startup class, and Automapper dependencies. This is why calling the whole startup class.</p>
  
  <p>Good Resources:</p>
  
  <p><a href="https://stackoverflow.com/questions/37724738/how-to-unit-test-asp-net-core-application-with-constructor-dependency-injection">how to unit test asp.net core application with constructor dependency injection</a></p>
  
  <p><a href="https://stackoverflow.com/questions/50921675/dependency-injection-in-xunit-project/50922017#50922017">Dependency injection in Xunit project</a></p>