Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
ShowAll Questionssorted byDate Posted
KapavariVenkatramana

# how to write 100% test class for Calculator CLass. Unit Testing on the Lightning Platform Write Negative Tests

how to write 100% test class for Calculator CLass. Unit Testing on the Lightning Platform  Write Negative Tests
Maharajan C
Hi,

Please use the below Apex Class and Test Class:

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

Thanks,
Maharajan.C

Maharajan C
Hi,

Please use the below Apex Class and Test Class:

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

Thanks,
Maharajan.C
This was selected as the best answer
KapavariVenkatramana
Thanks,
Maharajan.C

Hatsoff to Your more than 4 hours Sturggled. and You gave the Solution.Thanks a lot.
Bruno Araujo 2
It is not necessary to cover 100% of code. It is even redundt. Have a look at this (https://domyhomework.co.uk/do-my-accounting-homework) for a good example.
hd sd
This appears splendid! True, for me so far it looks very tough, but I am mastering. Together with our institution, we're working on a challenge to build how to calculate gst (https://gstinfo.net/) a comparable calculator for fixing geometry assignments.