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