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

huangapple go评论86阅读模式
英文:

How to make integration test fail when objectMapper throws exception

问题

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

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

@Test
void objectMapperTest(){
  // 逐行读取文件

  objname.foreach(obj -> {
    try{
      Result result = objectMapper.readValue(line,Result.class);
    } catch(IOException e) {
      // 如何使测试失败?
    }
  });
}
英文:

I am writing using JUNIT 5 junit-platform-console-standalone library to write integration tests. While reading a JSON object with Jackson, I wish to fail the test if there is any error in reading file.

Tried adding throws IOException to method signature but that doesn;t seem to work.

@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 ?  
    }
  });
}

答案1

得分: 0

你可以使用 fail()

@Test
void objectMapperTest(){
  // 逐行读取文件

  objname.foreach(obj -> {
    try{
      Result result = objectMapper.readValue(line,Result.class);
    } catch(IOException e) {
      Assertions.fail("测试失败信息");
    }
  });
}
英文:

You can use 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");
    }
  });
}

huangapple
  • 本文由 发表于 2020年8月28日 07:08:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63625289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定