0

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 the test class

  @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');
}   
}
1
  • 3
    A little bit of effort to indent your code more consistently would make a large improvement in its readability.
    – Adrian Larson
    Dec 26, 2018 at 21:42

2 Answers 2

3

To test this code path,

if (returnValue < 0) {
   throw new CalculatorException('Division returned a negative value.' + returnValue);
}

You're going to have to contrive input such that the if condition evaluates to true. What divided by what yields a negative value?

You already have a pattern in place for testing code paths that throw exceptions. You'd simply need to write a version of divide_throws_exception_for_division_by_zero() that calls this method with the input values that make returnValue < 0, and then makes assertions that check for the corresponding CalculatorException and message.

Additional Note

divide_throws_exception_for_division_by_two() and multiply_by_two() are not really testing anything. Both of them assert that true == true, trying to catch an exception in a code path that will not throw one, and fail to validate the results of the successful call that they make to divide() or multiply().

These tests do not need exception handlers because they are testing code that should not throw an exception. You should write assertions based on the expected behavior of this code, not tautologies that are guaranteed to pass.

0

I was running into the same issue. Write a positive test...

Integer two = 2; Integer four = 4;

System.AssertEquals(2,Calculator.divide(four,two));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .