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

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

How to make integration test fail when objectMapper throws exception

问题

  1. 我正在使用 JUNIT 5 `junit-platform-console-standalone` 库编写集成测试。在使用 Jackson 读取 JSON 对象时,如果在读取文件过程中出现任何错误,我希望使测试失败。
  2. 尝试将 throws IOException 添加到方法签名中,但似乎不起作用。
  3. @Test
  4. void objectMapperTest(){
  5. // 逐行读取文件
  6. objname.foreach(obj -> {
  7. try{
  8. Result result = objectMapper.readValue(line,Result.class);
  9. } catch(IOException e) {
  10. // 如何使测试失败?
  11. }
  12. });
  13. }
英文:

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.

  1. @Test
  2. void objectMapperTest(){
  3. // reading file line by line
  4. objname.foreach(obj -> {
  5. try{
  6. Result result = objectMapper.readValue(line,Result.class);
  7. } catch(IOException e) {
  8. //How to make the test fail ?
  9. }
  10. });
  11. }

答案1

得分: 0

你可以使用 fail()

  1. @Test
  2. void objectMapperTest(){
  3. // 逐行读取文件
  4. objname.foreach(obj -> {
  5. try{
  6. Result result = objectMapper.readValue(line,Result.class);
  7. } catch(IOException e) {
  8. Assertions.fail("测试失败信息");
  9. }
  10. });
  11. }
英文:

You can use fail()

  1. @Test
  2. void objectMapperTest(){
  3. // reading file line by line
  4. objname.foreach(obj -> {
  5. try{
  6. Result result = objectMapper.readValue(line,Result.class);
  7. } catch(IOException e) {
  8. Assertions.fail("Test failure message");
  9. }
  10. });
  11. }

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:

确定