如何在 objectMapper 抛出异常时使集成测试失败

How to make integration test fail when objectMapper throws exception

我正在使用 JUNIT 5 junit-platform-console-standalone 库编写集成测试。在用 Jackson 读取 JSON 对象时,如果读取文件有任何错误,我希望测试失败。

尝试将 throws IOException 添加到方法签名中,但这似乎不起作用。

@Test
void objectMapperTest(){
  // reading file line by line

  objname.foreach(obj -> {
    try{
      Result result = objectMapper.readValue(line,Result.class);
    } catch(IOException e) {
      //How to make the test fail ?  
    }
  });
}

您可以使用fail()

@Test
void objectMapperTest(){
  // reading file line by line

  objname.foreach(obj -> {
    try{
      Result result = objectMapper.readValue(line,Result.class);
    } catch(IOException e) {
      Assertions.fail("Test failure message");
    }
  });
}