der*_*itz 8 spring-test spring-boot
我有一个控制器和一个使用 @WebMvcTest 的测试并且它运行良好。现在我需要添加一些验证逻辑,为此我需要添加一个@Autowired额外的 bean(a @Component,一个 MapstructMapper)。
正如预期的那样,由于@WebMvcTest. (未发现任何组件)
有没有办法将一个 bean 添加到创建的上下文中?
由于我正在使用@MockBeans模拟服务层:有没有办法将所有模拟调用委托给真实对象?有了这个,我可以模拟映射器并委托给真正的映射器?!
DHR*_*SAL 14
在上下文中获取额外 bean 的一种简单方法是在测试类中使用嵌套的配置类
@TestConfiguration
static class AdditionalConfig {
@Bean
public SomeBean getSomeBean() {
return new SomeBean());
}
}
Run Code Online (Sandbox Code Playgroud)
例子:
场景 - 如果你有一些控制器说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;
}
}
Run Code Online (Sandbox Code Playgroud)
带有附加 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"));
}
}
Run Code Online (Sandbox Code Playgroud)
关于您的场景的其他想法:如果您真的打算对控制器类进行单元测试,那么理想情况下您应该模拟您正在测试的类的所有其他依赖项。理想情况下,单元测试的目的是只测试被测对象/类的行为。所有依赖类的行为或外部调用都应该被模拟。
当您开始在一个测试中同时测试多个类时,您将更多地转向组件测试或集成测试
一个非常简单的解决方案是通过@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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3223 次 |
| 最近记录: |