0

I am having an issue passing the Write Negative Tests Trailhead.

  • Create a new class named Calculator_Tests
  • Write positive and negative tests for the Calculator class
  • Be sure to use @testSetup methods where appropriate
  • Run your unit tests and confirm that the code coverage for calculator.cls is 100%

With everyone's help from This page I was able to achieve 100% code coverage - woohoo! But I haven't passed the module yet, because (I think) I haven't added any "@testSetup methods where appropriate". I'm not really sure what testsetup methods I would do given my current limited understanding, to create test data first?

Thank you for any help!!


  @isTest
    public static void addition() {
        System.AssertEquals(1,Calculator.addition(1,0), 'correct addition result');
    }

  @isTest
    public static void subtraction() {
        System.AssertEquals(1,Calculator.subtraction(1,0), 'correct subtraction result');
    }
    
  @isTest
    public static void multiply() {
        System.AssertEquals(6,Calculator.multiply(2,3), 'correct multiply result');
    }
    
  @isTest
    public static void divide() {
        System.AssertEquals(2,Calculator.divide(4,2), 'correct divide result');
    }

  @isTest
    public static void multiply_by_zero() {
        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 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_by_negative() {
        Boolean caught = false;
        try {
           Calculator.divide(12, -2);
        } catch (Calculator.CalculatorException e) {
            System.assertEquals('Division returned a negative value.', e.getMessage(), 'caught the right exception');
           caught = true;
        }
        System.assert(caught, 'threw expected exception');
    }
    
    
}

1 Answer 1

0

Quit the divide all Method in the Calculator Class, because the trailhead module test with you have 100% Coverage Only.

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

}

And for you class only add the name and dont evalue the Divide Method

@isTest
public class Calculator_Tests {
  @isTest
    public static void addition() {
        System.AssertEquals(1,Calculator.addition(1,0), 'correct addition result');
    }

  @isTest
    public static void subtraction() {
        System.AssertEquals(1,Calculator.subtraction(1,0), 'correct subtraction result');
    }
    
  @isTest
    public static void multiply() {
        System.AssertEquals(6,Calculator.multiply(2,3), 'correct multiply result');
    }
    

  @isTest
    public static void multiply_by_zero() {
        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');
    }
    
}

This work for me !

You must log in to answer this question.

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