+ Start a Discussion
MedhanieHabteMedhanieHabte 

Getting to 100 percent code coverage on my trailhead module “Write Negative Tests Unit”

I seem to be stuck on this trailhead module or write negative tests unit, while I have 93 percent code coverage, I can't seem to get the code coverage to hit 100 percent at the "returnValue" piece doesn't seem to hit.
My code is as follows.

Calculator Class
 
public class Calculator {
 public class CalculatorException extends Exception{}

  public static Integer addition(Integer a, Integer b){
   return a + b;
    }

   public static Integer subtraction(Integer a, Integer b){
    return a - b;
    }

 public static Integer multiply(Integer a, Integer b){
  if(b==0 || a==0){
  throw new CalculatorException('It doesn\'t make sense to multiply by 
   zero');
  }
  return a * b;
  }

 public static Decimal divide(Integer numerator, Integer denominator){
  if(denominator == 0){
  throw new CalculatorException('you still can\'t divide by zero');
   }
 Decimal returnValue = numerator / denominator;
  if(returnValue < 0){
    throw new CalculatorException('Division returned a negative value.' + 
 returnValue);
 }
   return returnValue;
  }


 }

And my test class as follows
 
@isTest
   public class Calculator_Tests {

@isTest
 public static void addition() {
    Calculator.addition(1, 0);
   }
@isTest
  public static void subtraction() {
    Calculator.subtraction(1, 0);
   }

@isTest
 public static void divide_throws_exception_for_division_by_zero() {
 Boolean caught = false;
 try {
    Calculator.divide(1, 0);
  } catch (Calculator.CalculatorException e) {
    System.assertEquals('you still can\'t divide by zero', e.getMessage(), 
  'caught the right exception');
    caught = true;
   }
   System.assert(caught, 'threw expected exception');
   }

  @isTest
 public static void divide_throws_exception_for_division_by_two() {
 Boolean caught = true;
 try {
    Calculator.divide(1, 2);
 } catch (Calculator.CalculatorException e) {
    System.assertEquals('you still can\'t divide by zero', e.getMessage(), 
  'caught the right exception');
    caught = true;
   }
   System.assert(caught, 'threw expected exception');
 }


@isTest
public static void multiply_by_one() {
  Boolean caught = false;
  try {
    Calculator.multiply(1, 0);
    } catch (Calculator.CalculatorException e) {
    System.assertEquals('It doesn\'t make sense to multiply by zero', 
    e.getMessage(), 'caught the right exception');
     caught = true;
    }
    System.assert(caught, 'threw expected exception');
  }

@isTest
 public static void multiply_by_two() {
  Boolean caught = true;
  try {
     Calculator.multiply(1, 2);
   } catch (Calculator.CalculatorException e) {
    System.assertEquals('It doesn\'t make sense to multiply by zero', 
  e.getMessage(), 'caught the right exception');
    caught = true;
   }
   System.assert(caught, 'threw expected exception');
}   
}

 
Best Answer chosen by MedhanieHabte
Abdul KhatriAbdul Khatri
How come a division returned a negative value with positive numbers? I don't think that is the right Scenario. 

Infact you can change that not accepting the negative value like this 
 
public static Decimal divide(Integer numerator, Integer denominator){
        if(denominator == 0){
            throw new CalculatorException('you still can\'t divide by zero');
        }
        if(numerator < 0 || denominator < 0)
        	throw new CalculatorException('negative value(s) not allowed.');
        
        Decimal returnValue = numerator / denominator;

        return returnValue;
    }

and add another test method
@isTest
    public static void divide_throws_exception_for_negative_number() {
        Boolean caught = true;
        try {
            Calculator.divide(-1, 2);
        } catch (Calculator.CalculatorException e) {
            System.assertEquals('negative value(s) not allowed.',e.getMessage());
            caught = true;
        }
        System.assert(caught, 'threw expected exception');
    }

I hope this will help.​

All Answers

Abdul KhatriAbdul Khatri
How come a division returned a negative value with positive numbers? I don't think that is the right Scenario. 

Infact you can change that not accepting the negative value like this 
 
public static Decimal divide(Integer numerator, Integer denominator){
        if(denominator == 0){
            throw new CalculatorException('you still can\'t divide by zero');
        }
        if(numerator < 0 || denominator < 0)
        	throw new CalculatorException('negative value(s) not allowed.');
        
        Decimal returnValue = numerator / denominator;

        return returnValue;
    }

and add another test method
@isTest
    public static void divide_throws_exception_for_negative_number() {
        Boolean caught = true;
        try {
            Calculator.divide(-1, 2);
        } catch (Calculator.CalculatorException e) {
            System.assertEquals('negative value(s) not allowed.',e.getMessage());
            caught = true;
        }
        System.assert(caught, 'threw expected exception');
    }

I hope this will help.​
This was selected as the best answer
KapavariVenkatramanaKapavariVenkatramana
@Abdul Khatri 

Now We are getting 93 % Code Coverage Only...Is there 100% Code coverage