【问题标题】:JUnit testing throw NullPointerExceptionJUnit 测试抛出 NullPointerException
【发布时间】:2021-03-19 19:34:45
【问题描述】:

我需要在 JUnit5(基于接口)中测试一个函数。我没有执行。如果函数的参数之一为空,我需要抛出 NullPointerException。我的问题是,如何测试这个函数来抛出 NullPointerException?谢谢!

【问题讨论】:

    标签: java testing junit junit5


    【解决方案1】:

    您能更清楚地解释问题吗?是什么

    不使用函数的输出(仅参数)?

    是什么意思?

    JUnit 可以测试方法是否抛出异常

    Exception exception = assertThrows(NumberFormatException.class, () -> {
            Integer.parseInt("1a");
        });
    

    【讨论】:

      猜你喜欢
      • 2017-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-12
      • 1970-01-01
      • 2020-12-07
      相关资源
      最近更新 更多