【问题标题】:How to create a bean of repository class如何创建存储库类的bean
【发布时间】:2016-05-01 11:24:57
【问题描述】:

我正在使用 spring mvc 测试框架进行单元测试。

以下是我的源代码: com.example.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。 但我不知道如何创建存储库的 bean。 请教我如何使用 Java(不是 xml)创建存储库类的 bean。

【问题讨论】:

  • 你导入Spring JPA配置类了吗?你如何配置 JPA?你能展示配置spring JPA(带有hibernate)的类吗?
  • 在我的项目中,没有Spring JPA配置类,只有Controller、Service、Repository和Domain类。我使用spring mvc框架。运行应用是没有问题的,但是运行测试代码的时候,出现了上述错误。
  • 使用@EnableJpaRepositories 注解为“单元测试”上下文定义的存储库接口创建实例和bean定义。
  • 你在使用 Mockito 吗?如果是这样,您可以在测试配置中将 Mockito 模拟列为 bean,并将其注入测试用例以从测试中进行操作。
  • @Shaheer 你能举个例子吗?谢谢

标签: java spring spring-mvc javabeans


【解决方案1】:

您需要在配置类中启用 JPA 存储库,指定包含存储库的包,如下所示

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

编辑:看起来你还没有定义 entityManager 和 dataSource。参考a tutorial here也可以回答类似问题here

【讨论】:

  • 感谢您的回复。但是当我添加@EnableJpaRepositories(basePackages = { "com.example" }) 时,发生另一个错误无法创建类型为 [org.springframework.orm.jpa.SharedEntityManagerCreator] 的内部 bean '(inner bean)#37654521' while setting bean属性“实体管理器”;
  • 以及更多错误:在设置构造函数参数时无法解析对 bean 'entityManagerFactory' 的引用;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
猜你喜欢
  • 2016-01-08
  • 1970-01-01
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 2017-11-24
  • 1970-01-01
  • 2018-11-08
  • 1970-01-01
相关资源
最近更新 更多