title: How to set up a unit test for spring data with service and repository?
body: <p>I have checked many SO comments and the docs for spring data and unit testing but I cant get this to work and I dont know why its not working.</p>
  
  <p>I have a junit test class that looks like this:</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(loader = AnnotationConfigContextLoader.class)
  public class DealServiceTest {
  
      @Configuration
      static class ContextConfiguration {
  
          // this bean will be injected into the OrderServiceTest class
          @Bean
          public DealService orderService() {
              DealService dealService = new DealService();
              // set properties, etc.
              return dealService;
          }
  
          @Bean
          public EmployeeService employeeService(){
              EmployeeService employeeService = new EmployeeService();
              return employeeService;
          }
  
      }
  
      @Autowired
      DealService dealService;
  
      @Autowired
      EmployeeService employeeService;
  
      @Test
      public void createDeal() throws ServiceException {
          Employee employee = new Employee("Daniel", "tuttle", "danielptm@me.com", "dannyboy", "secret password", 23.234, 23.23);
          Deal d = dealService.createDeal("ADSF/ADSF/cat.jpg", "A title goes here", "A deal description", 23.22, "Name of business", 23.23,23.23, employee, "USA" );
          Assert.assertNotNull(d);
  
      }
  </code></pre>
  
  <p>}</p>
  
  <p>And then I have my service class that looks like this</p>
  
  <pre><code>@Service
  public class DealService {
  
      @Autowired
      private DealRepository dealRepository;
  
      public Deal createDeal(String image, String title, String description, double distance, String location, double targetLat, double targetLong, Employee employee, String country) throws ServiceException {
          Deal deal = new Deal(image, title, description, distance, location, targetLat, targetLong, employee, country);
          try {
              return dealRepository.save(deal);
          }catch(Exception e){
              throw new ServiceException("Could not create a deal: "+deal.toString(), e);
          }
      }
  
      public Deal updateDeal(Deal d) throws ServiceException {
          try{
              return dealRepository.save(d);
          }catch(Exception e){
              throw new ServiceException("Could not update deal at this time: "+d.toString(),e);
          }
      }
  
      public List&lt;Deal&gt; getAllDealsForEmployeeId(Employee employee) throws ServiceException {
          try{
              return dealRepository.getAllDealsBy_employeeId(employee.getId());
          }catch(Exception e){
              throw new ServiceException("Could not get deals for employee: "+employee.getId(), e);
          }
      }
  
  }
  </code></pre>
  
  <p>And then my repository:</p>
  
  <p>*/</p>
  
  <pre><code>public interface DealRepository extends CrudRepository&lt;Deal, Long&gt;{
  
      public List&lt;Deal&gt; getDealsBy_country(String country);
  
      public List&lt;Deal&gt; getAllDealsBy_employeeId(Long id);
  
  }
  </code></pre>
  
  <p>My config file looks like this:</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories("com.globati.repository")
  @EnableTransactionManagement
  public class InfrastructureConfig {
  
      @Bean
      public DataSource dataSource() {
          HikariConfig config = new HikariConfig();
          config.setDriverClassName("com.mysql.jdbc.Driver");
          config.setJdbcUrl("jdbc:mysql://localhost:3306/DatabaseProject");
          config.setUsername("awesome");
          config.setPassword("database");
          return new HikariDataSource(config);
      }
  
  //  @Bean
  //  public DataSource derbyDataSource(){
  //      HikariConfig config = new HikariConfig();
  //      config.setDriverClassName("jdbc:derby:memory:dataSource");
  //      config.setJdbcUrl("jdbc:derby://localhost:1527/myDB;create=true");
  //      config.setUsername("awesome");
  //      config.setPassword("database");
  //
  //      return new HikariDataSource(config);
  //
  //  }
  
      @Bean
      public JpaTransactionManager transactionManager(EntityManagerFactory factory) {
          return new JpaTransactionManager(factory);
      }
  
      @Bean
      public JpaVendorAdapter jpaVendorAdapter() {
  
          HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
          adapter.setDatabase(Database.MYSQL);
          adapter.setGenerateDdl(true);
  
          return adapter;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  
          LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
          factory.setDataSource(dataSource()); //Get data source config here!
          factory.setJpaVendorAdapter(jpaVendorAdapter());
          factory.setPackagesToScan("com.globati.model");
  
          return factory;
      }
  }
  </code></pre>
  
  <p>But I get this error.</p>
  
  <blockquote>
    <p>java.lang.IllegalStateException: Failed to load ApplicationContext ...</p>
    
    <p>Caused by:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    qualifying bean found for dependency
    [com.globati.repository.DealRepository]: expected at least 1 bean
    which qualifies as autowire candidate. Dependency annotations:
    {@org.springframework.beans.factory.annotation.Autowired(required=true)}</p>
  </blockquote>
  
  <p>Any suggestions for how I can successfully do unit testing with spring data, junit and with my service and repositories would be greatly appreciated. Thanks!</p>