2016-01-25 85 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

相关问题