gpt4 book ai didi

java - Spring Boot 测试无法解决存储库依赖关系

转载 作者:行者123 更新时间:2023-11-30 01:47:39 27 4
gpt4 key购买 nike

我有一个 UserRepository,它是一个 CRUD 存储库,如下所示:

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

}

像这样的一个 UserController:

@RestController
@RequestMapping("/api")
public class UserController {

@Autowired
private UserRepository repository;
@Autowired
private UserResourceAssembler assembler;

和一个 WebMvcTest 类来测试我的 UserController:

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {

@Autowired
private MockMvc mvc;

@Test
public void getAllEmployeesAPI() throws Exception
{
mvc.perform( MockMvcRequestBuilders
.get("/api/users")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk());
}

}

当我运行服务器时一切都很好。但是,当我运行 maven-test 时出现此错误:

java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#7ba1cdbe' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7ba1cdbe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Cannot create inner bean '(inner bean)#7ba1cdbe' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7ba1cdbe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#7ba1cdbe': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available

最佳答案

如果你现在有相同的结构,我推荐@SpringBootTest

@WebMvcTest 不会将与数据库相关的 Bean 加载到应用程序上下文中。

关于java - Spring Boot 测试无法解决存储库依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57361434/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com