使用@WebMvcTest进行单元测试POST-@MockBean服务返回null [英] Unit Test POST with @WebMvcTest - @MockBean Service returns null

查看:1116
本文介绍了使用@WebMvcTest进行单元测试POST-@MockBean服务返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对控制器进行单元测试以保存Brand实体.在此测试中,我创建了一个我希望返回的Brand,然后将JSON发布到控制器.最初,我是依靠pass-by-reference的,所以我的控制器方法基本上是在做:

I'm trying to unit test a controller to save a Brand entity. In this test, I'm creating a Brand that I expect to get back and then I'm posting JSON to the controller. Originally, I was relying on pass-by-reference so my controller method was basically doing:

@Override
public ResponseEntity<MappingJacksonValue> save(@Valid @RequestBody Brand brand, BindingResult bindingResult) {

  validate(brand, null, bindingResult);
  if (bindingResult.hasErrors()) {
      throw new InvalidRequestException("Invalid Brand", bindingResult);
  }

  this.brandService.save(brand); // pass by reference
  MappingJacksonValue mappingJacksonValue = jsonView(JSON_VIEWS.SUMMARY.value, brand);
  return new ResponseEntity<>(mappingJacksonValue, HttpStatus.CREATED);
}

请注意,我实际上并没有使用该服务返回的Brand.当我以这种方式进行测试时,测试失败了,因为控制器返回了我传入的JSON,并且由于模拟了服务,所以控制器没有返回我期望的品牌,该品牌具有一个ID.因此,我更改了控制器以执行此操作:

Note that I wasn't actually using the returned Brand from the service. When I tested this way, my test was failing because the controller was returning the JSON I passed in, and since the service is mocked, the controller didn't return the brand I was expecting, which was to have an ID. So I changed the controller to do this:

brand = this.brandService.save(brand);

但是,当我调试时,从模拟服务返回的品牌为空.以下是我的测试.

However, when I debug, the brand returned from the mocked service is null. Below is my test.

@RunWith(SpringRunner.class)
@WebMvcTest(BrandController.class)
public class BrandSimpleControllerTest {

  @Autowire
  private MockMvc mockMvc;

  @MockBean
  private BrandService brandService;

  @Test
  public void testSave() throws Exception {
    Brand brand = new Brand();
    brand.setId(1L);
    brand.setName("Test Brand");

    when(this.brandService.save(brand)).thenReturn(brand);

    this.mockMvc.perform(this.post("/api/brands")
      .content("{\"name\": \"Test Brand\"}"))
      .andExpect(jsonPath("$.id", is(1)))
      .andExpect(jsonPath("$.name", is("Test Brand")));
  }

}

有什么建议吗?

推荐答案

好的,问题已解决.问题是您在服务调用中模拟的对象必须与传递给控制器​​的对象相同,因此当模拟查看预期的内容时,它会说哦,您给了我这个,所以您想要那个".这是使其工作的修改后的代码:

Alright, problem solved. The issue was that the object you mock in the service call has to be identical to the object passed into the controller so when the mock looks at what is expected, it says "oh, you gave me this and so you want that". Here's the modified code that makes it work:

Brand brand = new Brand();
brand.setId(1L);
brand.setName("Test Brand");
brand.setDateCreated(new LocalDateTime());
brand.setLastUpdated(new LocalDateTime());

// since all I'm passing into the controller is a brand name...
when(this.brandService.save(new Brand("Test Brand"))).thenReturn(brand);

这篇关于使用@WebMvcTest进行单元测试POST-@MockBean服务返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆