简体   繁体   English

Spring 启动 rest api 控制器单元测试

[英]Spring boot rest api unit test for cotroller

在此处输入图像描述

I have two controller user and role for crud, i have write test for user controller, but when i try to run test it give me the following error我有两个 controller 用户和 crud 角色,我为用户 controller 编写测试,但是当我尝试运行测试时,它给了我以下错误

"Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'uk.co.circuare.cube.service.user.repository.RoleRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}" “由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有'uk.co.circuare.cube.service.user.repository.RoleRepository'类型的合格bean可用:预计至少有1个有资格作为自动装配候选者的bean。依赖关系注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)}"

I am using mockito我正在使用 mockito

public class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper mapper;

    @MockBean
    private UserRepository repository; [![enter image description here][1]][1]

I have used two repository in my usercontroller我在我的用户控制器中使用了两个存储库

@Autowired private UserRepository repository; @Autowired 私有 UserRepository 存储库;

@Autowired private RoleRepository roleRepository; @Autowired private RoleRepository roleRepository;

The solution to this is pretty straightforward — you need to provide every bean used in your controller class for the Application Context in your test class.解决方案非常简单——您需要为测试 class 中的应用程序上下文提供 controller class 中使用的每个 bean。

You have mentioned an error saying that NoSuchBeanDefinitionException: No qualifying bean of type 'uk.co.circuare.cube.service.user.repository.RoleRepository' available: expected at least 1 bean which qualifies as autowire candidate.您提到了一个错误,指出NoSuchBeanDefinitionException: No qualifying bean of type 'uk.co.circuare.cube.service.user.repository.RoleRepository' available: expected at least 1 bean which qualifies as autowire candidate. . . It means that there is no bean of type RoleRepository in your test Application Context.这意味着您的测试应用程序上下文中没有RoleRepository类型的 bean。

You're using the @MockBean annotation.您正在使用@MockBean注释。 It will replace any existing bean of the same type in the application context.它将替换应用程序上下文中任何现有的相同类型的 bean。 If no bean of the same type is defined, a new one will be added.如果没有定义相同类型的bean,则会添加一个新的。 I would recommend you to check the details on the mock annotation, which you're using in this article: Mockito.mock() vs @Mock vs @MockBean我建议您检查您在本文中使用的模拟注释的详细信息: Mockito.mock() vs @Mock vs @MockBean

To resolve your issue, your test class should look like the following:要解决您的问题,您的测试 class 应如下所示:

public class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Autowired
    private ObjectMapper mapper;

    @MockBean
    private UserRepository repository;

    @MockBean
    private RoleRepository roleRepository;

    ...
}

You also have to mock RoleRepository in your test class.您还必须在测试 class 中模拟RoleRepository

@ExtendWith(MockitoExtension.class)
public class UserControllerTest {

    @InjectMocks
    private UserController userController;

    @Mock
    private ObjectMapper mapper;

    @Mock
    private UserRepository repository;

    @Mock
    private RoleRepository roleRepository;

    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM