Skip to main content

@All Here is the code

 @isTest public class Calculator_Tests { @isTest public static void additionPositiveTest() { integer a = 2; integer b = 3; integer c = Calculator.addition(a, b); System.assertEquals(5, c, 'expected result is 5'); } @isTest public static void substractionPositiveTest() { integer a = 6; integer b = 2; integer c = Calculator.subtraction(a, b); System.assertEquals(4, c, 'expected result is 4'); } @isTest public static void multiplyPositiveTest() { integer a = 2; integer b = 3; integer c = Calculator.multiply(a, b); System.assertEquals(6, c, 'expected result is 6'); } @isTest public static void dividePositiveTest() { integer a = 8; integer b = 2; decimal c = Calculator.divide(a, b); System.assertEquals(4, c, 'expected result is 4'); } @isTest public static void multiplyNegativeTest() { integer a = 0; integer b = 3; try { Calculator.multiply(a, b); } catch (Calculator.CalculatorException e) { System.assertEquals(e.getMessage(), 'It doesn\'t make sense to multiply by zero'); } } @isTest public static void divideByZeroTest(){ integer a = 5; integer b = 0; try { Calculator.divide(a, b); } catch (Calculator.CalculatorException e) { System.assertEquals(e.getMessage(), 'you still can\'t divide by zero'); } } @isTest public static void divideByNegativeTest() {      Test.startTest(); integer a = 3; integer b = -1; Boolean exceptionNeg = false; try { Calculator.divide(b, a); } catch (Calculator.CalculatorException e) { if (e.getMessage().contains('Division returned a negative value.')) { exceptionNeg = true; } System.assertEquals(false, exceptionNeg); }     Test.stopTest(); }      }   

5 respostas
  1. 23 de out. de 2021, 18:59

    Hi,

    try the following:

    Apex 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');

    }

    if(numerator < 0 || denominator < 0)

    throw new CalculatorException('negative value(s) not allowed.');

    Decimal returnValue = numerator / denominator;

    return returnValue;

    }

    }

    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');

    }

    @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');

    }

    }

    ref: https://probablesolution.blogspot.com/2021/10/trailhead-challenge-error-100-coverage.html

0/9000