向“@WebMvcTest”添加一个额外的bean

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

我有一个控制器和一个使用 @WebMvcTest 的测试,并且运行良好。现在我需要添加一些验证逻辑,为此我

@Autowired
一个额外的bean(一个
@Component
,一个MapstructMapper)。

正如预期的那样,由于

@WebMvcTest
,测试失败了。 (未发现任何组件)

有没有一种方法可以将一个bean添加到创建的上下文中?

由于我使用

@MockBeans
来模拟服务层:有没有办法将所有模拟调用委托给真实对象?有了这个我可以模拟映射器并委托给真正的映射器?!

java spring-boot spring-test
2个回答
24
投票

一个非常简单的解决方案是使用

@Import
来注释您的测试类,指定您想要在测试中使用的其他 bean 的类,如 文档中所述

通常 @WebMvcTest 与 @MockBean 或 @Import 结合使用 创建 @Controller beans 所需的任何协作者

例如

@WebMvcTest(MyController.class)
@Import(SomeOtherBean.class)
public class SourcingOrganisationControllerTests {

    // The controller bean is made available via @WebMvcTest  
    @Autowired
    private MyController myController;

    // Additional beans (only one in this case) are made available via @Import
    @Autowired
    private SomeOtherBean someOtherBean;
}

21
投票

在上下文中获取其他 bean 的一个简单方法是通过在测试类中使用嵌套配置类

@TestConfiguration
static class AdditionalConfig {
    @Bean
    public SomeBean getSomeBean() {
        return new SomeBean();
    }
}

示例:

场景 - 如果您有一些控制器,请说 ProductController 并且您有该类的相应切片测试,请说 ProductionControllerTest

@RestController
public class ProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private IProductValidator productValidator;


    @GetMapping("product")
    public Product getProduct(@RequestParam Long id) {

        Product product = productService.getProduct(id); // you are using mockBean of productService
        
        productValidator.validateProduct(product); // you need real bean of productValidator
        return product;
    }
}

具有附加 bean 配置的相应幻灯片测试类

@RunWith(SpringRunner.class)
@WebMvcTest
public class ProductControllerSliceTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private IProductService productService;

    @Autowired
    private ApplicationContext applicationContext;

    @TestConfiguration
    static class AdditionalConfig {
        @Bean
        public IProductValidator productValidator() {
            return new ProductValidator();
        }
    }


    @Test
    public void testProductGetById() throws Exception {
        Product testProductWithID1L = new Product(1L, "testProduct");
        when(productService.getProduct(anyLong())).thenReturn(testProductWithID1L);

        mockMvc.perform(get("/product")
                .param("id", "1")).andDo(print())
                .andExpect(status().isOk())
                .andExpect(jsonPath("name")
                        .value("testProduct"))
                .andExpect(MockMvcResultMatchers.jsonPath("id")
                        .value("1"));
    }
}

关于您的场景的其他想法: 如果您确实打算对控制器类进行单元测试,那么理想情况下您应该模拟您正在测试的类的所有其他依赖项。 理想情况下,单元测试的目的是仅测试被测对象/类的行为。所有依赖类的行为或外部调用都应该被模拟。
当您开始在一个测试下一起测试多个类时,您将更多地转向组件测试或集成测试

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