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

2 Answers 2

0

A @testSetup method is mostly a place for us to do things that require DML (i.e. inserting test data for some number of SObjects like Account). It's useful because it's something that (ostensibly, despite what the governor limits may lead us to believe) only gets executed once per test class. Less work being repeated for each test = tests run faster.

The challenge, at time of writing, doesn't mention @testSetup. The Calculator class also shouldn't have any "state" (variables inside of the class, but declared outside of all of the methods). There's nothing to be set up here. The methods only use the data being passed into them.

Instead, the issue here is most likely because you haven't followed the instructions for the challenge. From the challenge, emphasis mine

Write a negative test for the divide() method on the Calculator class. Call it testDivideByZero().

Trailhead is specifically looking for a test method with that name (and >= 20% code coverage).

-1

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 !

1
  • In general, "just remove some code" isn't an acceptable approach. Removing the division method defeats the entire point of the challenge (which literally says write a negative test __for the divide method__. This also doesn't touch on why OP's code isn't satisfying the challenge.
    – Derek F
    Mar 25 at 12:52

You must log in to answer this question.

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