【问题标题】:Unit test in java for method which catch exception and throw another modified exceptionjava中的单元测试,用于捕获异常并抛出另一个修改的异常的方法
【发布时间】:2016-01-04 05:42:16
【问题描述】:

我是单元测试新手。这是我第一次写单元测试。我正在尝试使用 TestNG 框架编写单元测试。

class A {
   public Return_type method1(param1, param2) {
     for(var : tillSomeValue) {
       try {
         value = someMethod(var);
       } catch (someException ex) {
          /* do some calculation here with the value of thrown exception */
            throw anotherException();
         }
    }
  }
}

我想为这个类编写单元测试。有人能给我一些线索吗?

【问题讨论】:

    标签: exception exception-handling mockito testng testng-eclipse


    【解决方案1】:
    1. 您需要测试两种情况 - success(成功的返回值)和 failures(异常),因此请相应地命名您的方法。

    尝试使用不同的测试方法制作成功和失败的场景。

    testMethod1_success() { }
    testMethod1_failure() { }
    testMethod1_success_paramBoundaries() { }  etc etc 
    
    1. 您的测试方法应尝试尽可能多地传达方法/预期值的输出,即如果它要返回值 10,则该值应在测试设置中固定并传达返回该值的原因。

    asserts 放在期望值上。

    1. 您需要为所有可能的逻辑路径和所有排列 - 输入值的组合 - param1 and param2 编写测试方法。

    为了获得更好的建议,请指定方法的返回类型或参数值。你没有列出那些。

    这就是我能想到的潜在客户,否则你可以在互联网上找到好的教程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多