Spring Boot @WebMvcTest中模拟的方法返回值为null

时间:2019-04-03 14:53:22

标签: java spring-boot spring-mvc mockito springrunner

以下测试失败,并显示: org.json.JSONException:无法解析的JSON字符串。 我尝试了模拟 saveAndFlush 方法

的各种变体
  // doAnswer(returnsFirstArg())。when(this.shipwreckRepository).saveAndFlush(shipwreck);

// when(this.shipwreckRepository.saveAndFlush(shipwreck))
// .then(returnsFirstArg());
 

但是,会弹出相同的错误,并且 MockHttpServletResponse 正文为空

  MockHttpServletResponse:
           状态= 200
    错误消息=空
          标头= {}
     内容类型=空
             身体=
 

如何正确模拟存储库 saveAndFlush 方法的返回值?

SpringBootHomeControllerTest.java

  @RunWith(SpringRunner.class)
@WebMvcTest(ShipwreckController.class)
公共类SpringBootShipwreckControllerTests {

@Autowired
私人MockMvc mvc;

@MockBean
私人ShipwreckRepository shipwreckRepository;


@测试
公共无效createShipwreck()引发异常{
    沉船沉船= generateSampleShipwreck();

    when(shipwreckRepository.saveAndFlush(shipwreck))。then(i-> i.getArgumentAt(0,Shipwreck.class));

    ObjectMapper映射器=新的ObjectMapper();
    字符串shipwreckJson = mapper.writeValueAsString(shipwreck);
    this.mvc.perform(post(“ / api / v1 / shipwrecks”)
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(shipwreckJson)
                     )

            .andExpect(status()。isOk())
            .andExpect(content()。json(shipwreckJson));
    验证(this.shipwreckRepository).saveAndFlush(shipwreck);


}

私人沉船generateSampleShipwreck(){
    整数深度= 10;
    双纬度= 1.0;
    双经= 1.0;
    整数yearDiscovered = 1;
    字符串名称=“名称”;
    字符串描述=“ desc”;
    字符串条件=“ cond”;
    沉船沉船=新沉船(1L,名称,描述,条件,深度,纬度,经度,yearDiscovered);
    返回沉船
}
 

}

ShipwreckRepository.java

 公共接口ShipwreckRepository扩展了JpaRepository  {

}
 

ShipwreckController.java

  @RestController
@RequestMapping(“ / api / v1 /”)
公共类ShipwreckController {

    @Autowired
    私人ShipwreckRepository shipwreckRepository;

    @RequestMapping(值=“沉船”,方法= RequestMethod.POST)
    公共沉船创建(@RequestBody沉船沉船){
        返回shipwreckRepository.saveAndFlush(shipwreck);
    }
}
 

Shipwreck.java

 包com.boot.model;

导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;

@实体
公共场合沉船{
    @ID
    @GeneratedValue(策略= GenerationType.AUTO)
    长ID;
    字符串名称;
    字符串描述;
    字符串条件;
    整数深度;
    双重纬度;
    双经度
    整数年发现;

    公共沉船(){}

    公共沉船(长ID,字符串名称,字符串描述,字符串条件,整数深度,双纬度,双经度,整数yearDiscovered){
        this.id = id;
        this.name =名称;
        this.description =说明;
        this.condition =条件;
        this.depth =深度;
        this.latitude =纬度;
        this.longitude =经度;
        this.yearDiscovered = yearDiscovered;
    }

    公共长getId(){
        返回ID;
    }

    公共无效setId(长ID){
        this.id = id;
    }

    公共字符串getName(){
        返回名称;
    }

    public void setName(String name){
        this.name =名称;
    }

    公共字符串getDescription(){
        返回说明;
    }

    公共无效setDescription(字符串描述){
        this.description =说明;
    }

    公共字符串getCondition(){
        退货条件;
    }

    公共无效setCondition(字符串条件){
        this.condition =条件;
    }

    public Integer getDepth(){
        返回深度
    }

    public void setDepth(Integer depth){
        this.depth =深度;
    }

    公开Double getLatitude(){
        返回纬度
    }

    public void setLatitude(Double latitude){
        this.latitude =纬度;
    }

    公开Double getLongitude(){
        返回经度
    }

    public void setLongitude(Double longitude){
        this.longitude =经度;
    }

    public Integer getYearDiscovered(){
        返回年份发现;
    }

    public void setYearDiscovered(Integer yearDiscovered){
        this.yearDiscovered = yearDiscovered;
    }
}
 

1 个答案:

答案 0 :(得分:1)

在when()和verify()指令中使用Mockito.any()而不是Shipwreck的特定实例。

受测试的控制器将接收到的Shipwreck实例将不是您创建的实例,而是通过反序列化调用它的json构建的一个新实例。因此,Mockito将不会响应指示使用前一个实例的指令。

替代方法:为沉船定义适当的equals()和hashCode()方法。