• Tonystk
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 3
    Replies
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');
}   
}

 

Hi,

 

We have a 3rd party webserivce that send leads from our site to SF with Lead Owner already assigned based on some coded rules. We now want to add a level of lead assignment above what this can handle, so I wrote a lead assignment rule and have activated it.

 

The rules seem to be completley ignored as leads are coming in, but work if a lead is edited and saved ticking the "use active assignmnet rules" checkbox. (i.e the lead assignment crieria works, but its not being triggered)

 

So I thought a trigger to re-run lead assignment directly after a lead has been created would be the only solution, issue is I have no coding experience (aside from one easy update trigger some guys on here helped with!)

 

Can anyone help? Or if another solution exists that woudl be great too!

 

Antony