title: AutoMapper+xUnit: Missing type map configuration or unsupported mapping
body: <p>I cannot figure this one out. I have a N-Tier ASP.MVC application and I am writing my first Unit Test and it seems to fail on my AutoMapper configuration. I have used AutoMapper a million times and never had any problems using it.</p>
  
  <p>I'm sure I am missing something simple, but I have been staring at this for 24 hours now.</p>
  
  <h2>Class Library: APP.DOMAIN</h2>
  
  <pre><code>public class User : IEntity&lt;int&gt;
  {
      public int Id { get; set; }
  
      [StringLength(20), Required]
      public string UserName { get; set; }
  }
  </code></pre>
  
  <h2>Class Library: APP.SERVICE</h2>
  
  <p><strong>References App.Domain</strong></p>
  
  <pre><code>public class UserViewModel
  {
      public int Id { get; set; }
      public string UserName { get; set; } 
  }
  </code></pre>
  
  <p>I have my AutoMapper bootstrapper in the service layer.    </p>
  
  <pre><code>public static class AutoMapperBootstrapper
  {
      public static void RegisterMappings()
      {
          Mapper.CreateMap&lt;User, UserViewModel&gt;();
      }
  }
  </code></pre>
  
  <p><strong>UserService.cs</strong></p>
  
  <pre><code> public class UserService : IUserService
   {
      private readonly IUserRepository _userRepository;
  
      public UserService(IUserRepository userRepository)
      {
          _userRepository = userRepository;
      }
  
      public List&lt;UserViewModel&gt; GetUsers()
      {
          var users = _userRepository.GetAll();
  
          if (users == null)
          {
              throw new Exception("No users found.");
          }
  
          return Mapper.Map&lt;List&lt;UserViewModel&gt;&gt;(users); // FAILS ON AUTOMAPPER
      }
    }
  </code></pre>
  
  <h2>ASP.MVC Layer: APP.WEB</h2>
  
  <p><strong>References App.Service</strong></p>
  
  <pre><code>private void Application_Start(object sender, EventArgs e)
  {
      // Register AutoMapper
      AutoMapperBootstrapper.RegisterMappings();
      Mapper.AssertConfigurationIsValid();
  
      // Code that runs on application startup
      AreaRegistration.RegisterAllAreas();
      GlobalConfiguration.Configure(WebApiConfig.Register);
      RouteConfig.RegisterRoutes(RouteTable.Routes);
  }
  </code></pre>
  
  <h2>Unit Test Layer:</h2>
  
  <pre><code>public class TestUserRepository :IUserRepository
  {
      public IEnumerable&lt;User&gt; GetAll()
      {
          var users = new List&lt;User&gt;()
          {
              new User { Id = 1, UserName = "Mary"},
              new User { Id = 2, UserName = "Joe"}
          };
          return users;
      }
    }
  
  
  public class UserServiceTest
  {
      private IUserService _userService;
      private readonly IUserRepository _userRepository;
  
      public UserServiceTest()
      {
          _userRepository = new TestUserRepository();
      }
  
      [Fact]
      public void GetUsers_Should_Return_Correct_Number_Of_Users()
      {
          // Arrange
          _userService = new UserService(_userRepository);
  
          // Act
          var result = _userService.GetUsers(); // FAILS ON AUTOMAPPER
  
          // Assert
          Assert.True(result.Any(u =&gt; u.UserName == "Mary")); 
      }
  }
  </code></pre>
  
  <h2>Failing Test Message:</h2>
  
  <pre><code>*** Failures ***
  
  Exception
  AutoMapper.AutoMapperMappingException: AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping.
  
  Mapping types:
  User -&gt; UserViewModel
  App.Data.Model.User -&gt; App.Service.ViewModels.UserViewModel
  
  Destination path:
  List`1[0]
  
  Source value:
  App.Data.Model.User
     at App.Service.Services.UserService.GetUsers() in D:\Repositories\App\App.Service\Services\UserService.cs:line 36
     at App.Tests.Service.Tests.UserServiceTest.GetUsers_Should_Return_Correct_Number_Of_Users() in D:\Repositories\App\App.Tests\Service.Tests\UserServiceTest.cs:line 34
  </code></pre>