title: Owin Testing (Self hosting) not registering web api routes when using DI
body: <p>I am trying to implement <a href="https://www.nuget.org/packages/Microsoft.Owin.Testing/3.0.1" rel="nofollow">Microsoft.Owin.Testing/3.0.1</a> for WebApi 2 integration tests. Everything works fine except when I add <a href="http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2" rel="nofollow">attribute routing</a>. The routes are not recognized or I am unable to register them properly. I'm pretty sure it has something to do with api controller dependency injection that is not used when testing.</p>
  
  <p>My values api controller</p>
  
  <pre><code> [RoutePrefix("api")]
          public class ValuesApiController : ApiController
          {
              private readonly ValuesControllerService _valuesControllerService;
  
              public ValuesApiController(ValuesControllerService valuesControllerService)
              {
                  _valuesControllerService = valuesControllerService;
              }
  
              // GET api/values
              [HttpGet]
              [Route("values")]
              public async Task&lt;IHttpActionResult&gt; Get()
              {
                  if (_valuesControllerService == null)
                      throw new ArgumentException("valuesControllerService");
  
                  return Ok(new[] { "value1", "value2" });
              }
          }
  </code></pre>
  
  <p>I am using Castle Windsor to resolve API Controllers using (<a href="https://github.com/jvanderbiest/OwinTestingWebApi/tree/master/OwinTestingWebApi/IoC" rel="nofollow">source</a>):</p>
  
  <pre><code>GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), new WindsorCompositionRoot(_container));
  ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(_container));
  </code></pre>
  
  <p>I'm registering my WebApi routes in Global.asax so it is registered properly when running the application (and not the tests).</p>
  
  <pre><code>public class WebApiApplication : HttpApplication
      {
          protected void Application_Start()
          {
              GlobalConfiguration.Configure(WebApiConfig.Register);
          }
      }
  </code></pre>
  
  <p>When testing, I call the registration after Startup. Here is a sample test which I use (<a href="https://github.com/jvanderbiest/OwinTestingWebApi/blob/master/OwinTestingWebApi.Tests/Integration/ValuesControllerTests.cs" rel="nofollow">test source</a>):</p>
  
  <pre><code>[Test]
          public async void GetValueTestForStartup1()
          {
              using (var server = TestServer.Create&lt;Startup1&gt;())
              {
                  using (var client = new HttpClient(server.Handler))
                  {
                      var request = await client.GetAsync(_url);
  
                      Assert.IsTrue(request.IsSuccessStatusCode);
                  }
              }
          }
  </code></pre>
  
  <p>And here are some Startup implementations I tried:</p>
  
  <pre><code>internal class Startup1
          {
              public void Configuration(IAppBuilder app)
              {
                  new Startup().ConfigureForIntegrationTests(app, x =&gt; x.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
                  GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
                  GlobalConfiguration.Configure(WebApiConfig.Register);
              }
          }
  </code></pre>
  
  <blockquote>
    <p>404 when executing a request</p>
  </blockquote>
  
  <pre><code>internal class Startup2
  {
      public void Configuration(IAppBuilder app)
      {
          new Startup().ConfigureForIntegrationTests(app, x =&gt; x.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
          GlobalConfiguration.Configure(WebApiConfig.Register);
          GlobalConfiguration.Configuration.EnsureInitialized();
      }
  }
  </code></pre>
  
  <blockquote>
    <p>System.InvalidOperationException : This method cannot be called during
    the application's pre-start initialization phase.</p>
  </blockquote>
  
  <pre><code>internal class Startup3
      {
          public void Configuration(IAppBuilder app)
          {
              new Startup().ConfigureForIntegrationTests(app, y =&gt; y.Kernel.ComponentModelBuilder.AddContributor(new SingletonEqualizer()));
              // Web API routes
  
              GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
  
              GlobalConfiguration.Configuration.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional }
              );
  
              app.UseWebApi(GlobalConfiguration.Configuration);
              GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), new TestWebApiResolver());
          }
      }
  </code></pre>
  
  <blockquote>
    <p>500 Internal Server error (probably because there is no parameterless
    constructor found).</p>
  </blockquote>
  
  <p>Complete sourceproject is located here:
  <a href="https://github.com/jvanderbiest/OwinTestingWebApi" rel="nofollow">https://github.com/jvanderbiest/OwinTestingWebApi</a></p>