Reference no: EM133703028
Create a Calculator.java class. Create following 4 static methods (with return type float):
Add(), Subtract(), Multiply() and Divide()
Each of the above methods should take two arguments and return the final result after the operation.
Create a test class now with a name CalculatorTest.java. In this class perform the following steps:
Import the following libraries at the top of this file:
import org.JUnit.Before;
import org.JUnit.Test;
import static org.JUnit.Assert.assertEquals;
Create the object of Calculator.java class by writing the following code in CalculatorTest.java class.
private Calculator calculator;
@Before
public void setUp() {
this.calculator = new Calculator();
}
Now, generate the JUnit test classes for all the methods one by one. Declare and initialize the two variables to pass those to the 4 methods. Store the expected result in one of the variables and then compare it with the actual returned value of the specific function.
After creating 4 JUnit test cases for addition, subtraction, multiplication, and division, run all the test cases one by one. Check the output. All the test cases should be passed at the end.