这是我下面的代码示例,我正在尝试模拟一个单元测试用例:
public create Test(){
ResponseEntity<Create> result = restTemplate.exchange(.....,Create.class);
if(result.getStatusCode !=null){
return result.getBody.id;
}
else {
throw new Runtime Exception();
}
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Create{
public String id;
public String[] errors;
public boolean success;
}
我正在编写的模拟测试用例在if语句处抛出了一个空Poniter异常。到目前为止,这是我尝试过的:
Create create = new Create();
create = "id";
create.errors = "errors";
create.success = "success" != null;
Mockito.when(exchange.getStatusCode()..equals(HttpStatus.CREATED)).thenReturn(null);
我已经成功地模拟了结果并进行了模板交换,但失败了,尤其是在if语句行中。 任何帮助将不胜感激。
答案 0 :(得分:0)
您将“ id”归因于“创建”对象,并且在getStatusCode()
之后使用双点。正确的应该是:
create.id = "id";
create.errors = "errors";
create.success = "success" != null;
Mockito.when(exchange.getStatusCode().equals(HttpStatus.CREATED)).thenReturn(null);