MockMVC 和 Spring boot 集成测试出错

问题描述 投票:0回答:1

我正在尝试为我的 Spring boot 应用程序创建集成测试。为此,我遵循了以下教程:https://programandoenjava.com/pruebas-de-integracion-en-spring-boot/

这是我的代码:

@SpringBootTest(classes = ArticulosControllerImpl.class)
@AutoConfigureMockMvc
public class ArticulosControllerIT {
    @Autowired
    private MockMvc mockMvc;
    private final String ARTICULOS_BASE_URL = "/articulos";

    @Test
    public void CUANDO_se_llama_a_articulos_ENTONCES_el_estado_es_200() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get(ARTICULOS_BASE_URL))
                .andExpect(status().isOk())
                .andExpect(MockMvcResultMatchers
                        .content()
                        .contentTypeCompatibleWith(MediaType.APPLICATION_JSON));
    }
}

但是在执行测试时出现以下错误。我想这是因为在我的控制器中我使用的是 ArticulosService bean,但我不知道如何修复它,并且在同一服务中我正在使用其他 bean。请问有什么解决办法吗?

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.ulises.mybartpv.articulos.rest.controllers.impl.ArticulosControllerImpl required a bean of type 'com.ulises.mybartpv.articulos.services.ArticulosService' that could not be found.


Action:

Consider defining a bean of type 'com.ulises.mybartpv.articulos.services.ArticulosService' in your configuration.

谢谢

java spring spring-boot junit5 mockmvc
1个回答
0
投票

如果您不需要在此特定测试中测试您的 bean 逻辑,您可能应该使用

@MockBean
注释来模拟您的 bean。

您可以按照本指南进行操作:https://spring.io/guides/gs/testing-web

© www.soinside.com 2019 - 2024. All rights reserved.