2016-01-25 84 views
0

我正在使用spring mvc測試框架進行單元測試。如何創建存儲庫類的bean

下面是我的源代碼: com.exmple.main

MyController.java

@Controller 
public class MyController { 

    @Autowired 
    private MyService myService; 

    @RequestMapping(method = RequestMethod.POST) 
    @ResponseBody 
    public Map<Object, Object> myControllerFunction(@RequestBody final Object jsonRequest) { 
     /* do something */ 

     return response; 
    } 
} 

MyRepository.java

@Repository 
public interface MyRepository extends JpaRepository<My, String> { 

    @Query(value="select * from my d where (d.start_date<to_date(:date,'YYYY/DD/MM')) and (d.end_date>to_date(:date,'YYYY/DD/MM'))", nativeQuery=true) 
    List<My> findByDate(@Param("date") String date); 
} 

MyService.java

public interface MyService { 
    List<My> findByDate(String date); 
} 

MyServiceImpl.java

@Service 

public class MyServiceImpl implements MyService { 
    @Autowired 
    MyRepository destRepo; 

    @Override 
    public List<My> findByDate(String date) { 
     List<My> listDest = destRepo.findByDate(date); 

     return listDest; 
    } 
} 

com.example.test

MyControllerTest.java

@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 { 

    } 
} 

TestConfig.java

@Configuration 
public class TestConfig { 
    @Bean 
    public MyService myService() { 
     // set properties, etc. 
     return new MyServiceImpl(); 
    } 
} 

當我運行測試,會顯示以下錯誤 嵌套的例外是org.springframework.beans.factory.NoSuchBeanDefinitionException

我知道發生了異常,因爲爲MyService沒有發現MyRepository的任何豆。 但我不知道如何創建一個存儲庫的bean。 請教我如何使用Java(而不是xml)創建存儲庫類的bean。

+0

您是否導入了Spring JPA配置類?你如何配置JPA?你可以顯示配置Spring JPA的類(有休眠)嗎? – Shaheer

+0

在我的項目中,沒有Spring JPA配置類,只有Controller,Service,Repository和Domain類。我使用spring mvc框架。運行應用程序沒有問題,但是在運行測試代碼時發生了上述錯誤。 – NhanQV

+0

使用@EnableJpaRepositories註釋爲爲「單元測試」上下文定義的存儲庫接口創建實例和bean定義。 – Shaheer

回答

0

您需要啓用JPA庫在你的配置類,指定包含庫如下

@Configuration 
@EnableJpaRepositories(basePackages = { 
    "com.example.repository" 
}) 
public class TestConfig { 
    @Bean 
    public MyService myService() { 
     // set properties, etc. 
     return new DestinationServiceImpl(); 
    } 
} 

編輯軟件包:看起來你還沒有定義的EntityManager和數據源。請參閱a tutorial here並且還回答類似的問題here

+0

謝謝您的答覆。但是,當我加入@EnableJpaRepositories(basePackages = { 「com。示例」 })時,發生其他錯誤無法創建內部bean '(內部bean)#37654521' 類型的[org.springframework.orm.jpa.SharedEntityManagerCreator],而設置bean屬性'entityManager'; – NhanQV

+0

多的錯誤:無法解析參考豆「的entityManagerFactory」,同時設置構造函數的參數;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有定義名爲'entityManagerFactory'的bean – NhanQV

+0

http://stackoverflow.com/questions/19653391/cannot-create-inner-bean-inner-bean-of-type- ORG-springframework的-ORM,JPA-SHA – sidgate

相關問題