title: How to create a bean of repository class
body: <p>I'm doing unit test using spring mvc test framework.</p>
  
  <p>The following is my source code:
  com.exmple.main</p>
  
  <p><strong>MyController.java</strong></p>
  
  <pre><code>@Controller
  public class MyController {
  
      @Autowired
      private MyService myService;
  
      @RequestMapping(method = RequestMethod.POST)
      @ResponseBody
      public Map&lt;Object, Object&gt; myControllerFunction(@RequestBody final Object jsonRequest) {
          /* do something */
  
          return response;
      }
  }
  </code></pre>
  
  <p><strong>MyRepository.java</strong></p>
  
  <pre><code>@Repository
  public interface MyRepository extends JpaRepository&lt;My, String&gt; {
  
      @Query(value="select * from my d where (d.start_date&lt;to_date(:date,'YYYY/DD/MM')) and (d.end_date&gt;to_date(:date,'YYYY/DD/MM'))", nativeQuery=true)
      List&lt;My&gt; findByDate(@Param("date") String date);
  }
  </code></pre>
  
  <p><strong>MyService.java</strong></p>
  
  <pre><code>public interface MyService {
      List&lt;My&gt; findByDate(String date);
  }
  </code></pre>
  
  <p><strong>MyServiceImpl.java</strong></p>
  
  <pre><code>@Service
  
  public class MyServiceImpl implements MyService {
      @Autowired
      MyRepository destRepo;
  
      @Override
      public List&lt;My&gt; findByDate(String date) {
          List&lt;My&gt; listDest = destRepo.findByDate(date);
  
          return listDest;
      }
  }
  </code></pre>
  
  <p>com.example.test</p>
  
  <p><strong>MyControllerTest.java</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes={TestConfig.class})
  @WebAppConfiguration
  public class MyControllerTest {
      private MockMvc mockMvc;
  
      @Autowired
      MyService myService;
  
      @Autowired
      protected WebApplicationContext webApplicationContext;
  
      @Before
      public void setup() throws Exception {
          // this.mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
          mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
      }
  
      @Test
      public void listAllMy() throws Exception {
  
      }
  }
  </code></pre>
  
  <p><strong>TestConfig.java</strong></p>
  
  <pre><code>@Configuration
  public class TestConfig {
      @Bean
      public MyService myService() {
          // set properties, etc.
          return new MyServiceImpl();
      }
  }
  </code></pre>
  
  <p>When I run test, the following error is displayed
  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException</p>
  
  <p>I know the exception occurred because MyService didn't find any bean of MyRepository.
  But I don't know how to create a bean of repository.
  Please teach me how to create a bean of repository class using Java (not xml).</p>