title: unable to launch spring-data-rest unit test, RepositoryRestResource not available for @Autowire
body: <p>I'm porting an app across from JDBC / REST to <code>spring-data-rest</code> and my one and only unit test with fails with the error</p>
  
  <pre><code>NoSuchBeanDefinitionException: 
      No qualifying bean of type 'com.xxx.repository.ForecastRepository' available
  </code></pre>
  
  <p>The app was retro-fitted with <code>spring-boot</code> just previously, and now I'm trying to put a new layer in place with <code>spring-data-rest</code> on top of <code>spring-data-jpa</code>.</p>
  
  <p>I'm attempting to work out the correct Java-config according to </p>
  
  <p><a href="https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4" rel="nofollow noreferrer">Custom Test Slice with Spring Boot 1.4</a> </p>
  
  <p>but I had to deviate from the idiomatic approach because </p>
  
  <ol>
  <li>the <code>@WebMvcTest</code> annotation doesn't suppress the security module which causes the test to fail</li>
  <li>the <code>@MockMvcAutoConfiguration</code> fails due to missing dependencies unless I specify <code>@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)</code> (see <a href="https://stackoverflow.com/questions/39213531/spring-boot-test-unable-to-inject-testresttemplate-and-mockmvc">here</a>)</li>
  <li><code>@WebMvcTest</code> and <code>@SpringBootTest</code> are mutually exclusive since they both specify <code>@BootstrapWith</code> and can't run together</li>
  </ol>
  
  <p>So this is the closest I've got but Spring can't locate my <code>@RepositoryRestResource</code> repository:</p>
  
  <p><strong>Repository</strong></p>
  
  <pre><code>@RepositoryRestResource(collectionResourceRel = "forecasts", path = "forecasts")
  public interface ForecastRepository extends CrudRepository&lt;ForecastExEncoded,
          Long&gt; {
  </code></pre>
  
  <p><strong>JUnit Test</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK,
          classes = {TestRestConfiguration.class})
  public class ForecastRestTests {
  
      @Autowired
      private MockMvc mockMvc;
  
      @Autowired
      private ForecastRepository forecastRepository;
  
      @Before
      public void deleteAllBeforeTests() throws Exception {
          forecastRepository.deleteAll();
      }
  
      @Test
      public void shouldReturnRepositoryIndex() throws Exception {
  
          mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk()).andExpect(
                  jsonPath("$._links.forecasts").exists());
      }
  
  }
  </code></pre>
  
  <p><strong>Configuration</strong></p>
  
  <pre><code>@OverrideAutoConfiguration(enabled = false)
  @ImportAutoConfiguration(value = {
          RepositoryRestMvcAutoConfiguration.class,
          HttpMessageConvertersAutoConfiguration.class,
          WebMvcAutoConfiguration.class,
          MockMvcAutoConfiguration.class,
          MockMvcSecurityAutoConfiguration.class
  })
  @Import({PropertySpringConfig.class})
  public class TestRestConfiguration {}
  </code></pre>
  
  <p><strong>Also tried...</strong> </p>
  
  <p>I tried to configure the unit test with just <code>@WebMvcTest</code> and this <code>@ComponentScan</code> below from <a href="https://stackoverflow.com/a/35200644/1385174">How to exclude AutoConfiguration from Spring Boot</a>), in an attempt to simplify it all, however the <code>excludeFilters</code> had no effect.</p>
  
  <pre><code>@ComponentScan(
          basePackages="com.xxx",
          excludeFilters = {
                  @ComponentScan.Filter(type = ASSIGNABLE_TYPE,
                          value = {
                                  SpringBootWebApplication.class,
                                  JpaDataConfiguration.class,
                                  SecurityConfig.class
                          })
          })
  </code></pre>
  
  <p>I've set Spring's logging to trace because all I can do at this point is try to find clues as to what is happening from log output. So far though without any luck. </p>
  
  <p>I can see in the logging that <a href="http://docs.spring.io/spring-data/rest/docs/current/api/org/springframework/data/rest/core/config/RepositoryRestConfiguration.html" rel="nofollow noreferrer">RepositoryRestConfiguration</a> is loading, but obviously it isn't fed with the right info and I am unable to work out how that is done, after googling and pouring over the Spring docs and API. I think I must have read every relevant question here on SO .</p>
  
  <p><strong>Update 2016-11-16 10:00</strong></p>
  
  <p>One thing I see in the logs which concerns me is this:</p>
  
  <pre><code>Performing dependency injection for test context  [DefaultTestContext@2b4a2ec7 [snip...]
   classes = '{class com.xxx.TestRestConfiguration, 
      class com.xxx.TestRestConfiguration}', 
  </code></pre>
  
  <p>i.e. the context lists the configuration class <strong><em>twice</em></strong>. I specified the config class (once only) on the <code>@SpringBootTest#classes</code> annotation. But if I leave off the <code>#classes</code> from the annotation, Spring Boot finds and pulls in all the config via the <code>@SpringBootApplication</code> class.</p>
  
  <p>So is that a hint that I am specifying the configuration in the wrong place? How else would I do it?</p>