-1

I am new with unit test. This is the first time I am writing unit test. I am trying to write Unit tests using TestNG framework.

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();
         }
    }
  }
}

I want to write Unit tests for this class. Can some give me some leads?

rshubh
  • 1

1 Answers1

0
  1. You need to test both scenarios - success ( a successful return value ) and failures ( Exception ) so name your methods accordingly.

Try to make success and failure scenarios from separate test methods.

testMethod1_success() { }
testMethod1_failure() { }
testMethod1_success_paramBoundaries() { }  etc etc 
  1. Your test method should try to communicate output of method / expected value as much as possible i.e. if its going to return a value 10 then that value should be fixed in test set up and communicate reasons for returning for that value.

Put asserts on expected value.

  1. You need to write test methods for all possible logical paths and all permutations - combinations of input values - param1 and param2.

For better suggestions, do specify return type or parameter values of your method. You have not listed those.

This is what all I can think of in terms of leads, otherwise you can find good tutorials on Internet.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98