小编Pal*_*Dot的帖子

使用 Junit5 和 Mockito 比较 2 个 Json 字符串

我有下面的 Junit.i 正在尝试测试比较两个 Json 字符串是否具有相同的字段(顺序和值并不重要)。我的测试不断失败并出现以下错误

org.opentest4j.AssertionFailedError:预期:<{“person_id”:0,“person_name”:null}>但是:<{“person_id”:0,“person_name”:null}

@Test 
  void inputpojo_test() throws Exception {
  
  String path = "src/test/data/json_file.txt";
  String jsonString = new String(Files.readAllBytes(Paths.get(path)));
  
   Input i = new Input();
  
  ObjectMapper objectMapper = new ObjectMapper(); 
  String bean =objectMapper.writeValueAsString(i);
  
  assertEquals(bean,jsonString); 
 }
Run Code Online (Sandbox Code Playgroud)

json_file.txt 是{"person_id":0,"person_name":null},输入类是

@JsonProperty("person_id") //getters and setter ommited for brevity
     int id;
    
    @JsonProperty("person_name")
     String name ;
    
    @JsonIgnore
    String value ;
Run Code Online (Sandbox Code Playgroud)

java junit mockito jackson junit5

1
推荐指数
1
解决办法
5873
查看次数

标签 统计

jackson ×1

java ×1

junit ×1

junit5 ×1

mockito ×1