Java 如何创建存储库类的bean
我正在使用SpringMVC测试框架进行单元测试 以下是我的源代码: com.example.main MyController.javaJava 如何创建存储库类的bean,java,spring,spring-mvc,javabeans,Java,Spring,Spring Mvc,Javabeans,我正在使用SpringMVC测试框架进行单元测试 以下是我的源代码: com.example.main MyController.java @Controller public class MyController { @Autowired private MyService myService; @RequestMapping(method = RequestMethod.POST) @ResponseBody public Map<Objec
@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;
}
}
@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);
}
public interface MyService {
List<My> findByDate(String date);
}
@Service
public class MyServiceImpl implements MyService {
@Autowired
MyRepository destRepo;
@Override
public List<My> findByDate(String date) {
List<My> listDest = destRepo.findByDate(date);
return listDest;
}
}
@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 {
}
}
@Configuration
public class TestConfig {
@Bean
public MyService myService() {
// set properties, etc.
return new MyServiceImpl();
}
}
TestConfig.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;
}
}
@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);
}
public interface MyService {
List<My> findByDate(String date);
}
@Service
public class MyServiceImpl implements MyService {
@Autowired
MyRepository destRepo;
@Override
public List<My> findByDate(String date) {
List<My> listDest = destRepo.findByDate(date);
return listDest;
}
}
@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 {
}
}
@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类。如果需要在配置类中启用JPA存储库,请指定包含存储库的包,如下所示
@Configuration
@EnableJpaRepositories(basePackages = {
"com.example.repository"
})
public class TestConfig {
@Bean
public MyService myService() {
// set properties, etc.
return new DestinationServiceImpl();
}
}
编辑:看起来您还没有定义entityManager和dataSource。请参阅并回答类似问题是否导入了Spring JPA配置类?您如何配置JPA?你能展示配置SpringJPA的类吗(使用hibernate)?在我的项目中,没有SpringJPA配置类,只有控制器、服务、存储库和域类。我使用SpringMVC框架。运行应用程序没有问题,但在运行测试代码时,出现了上述错误。请使用@EnableJpaRepositories注释为“单元测试”上下文定义的存储库接口创建实例和bean定义。是否使用Mockito?如果是这样,您可以在测试配置中将Mockito mock列为bean,并将其注入测试用例中,以便从测试中进行操作。@Shaheer您能给我一个例子吗?谢谢你的回复。但是当我添加@EnableJpaRepositories(basePackages={“com.example”})时,在设置bean属性“entityManager”时,出现了另一个错误,即无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean'(内部bean)35654521';还有一个错误:设置构造函数参数时无法解析对bean“entityManagerFactory”的引用;嵌套异常为org.springframework.beans.factory.NoSuchBean定义异常:未定义名为“entityManagerFactory”的bean