Question
package Calculator; /** * Calculator class * @author dhruvdh * @version 0.1 */ public class Calculator { int lhs, rhs; /** * Constructor for Calulator
package Calculator;
/**
* Calculator class
* @author dhruvdh
* @version 0.1
*/
public class Calculator {
int lhs, rhs;
/**
* Constructor for Calulator
* @param l Left hand side of the equation
* @param r Right hand side of the equation
*/
public Calculator(int l, int r) {
lhs = l;
rhs = r;
}
/**
* Add two numbers and return the result
* @return int
*/
public int add() {
return lhs + rhs;
}
****************************************************************************************************
Calculator.Calculator class is fully implemented and contains a list of methods for each basic arithmetic operation. The only method that is different from what youd expect is divide(), which throws an exception if someone tries to divide by zero.
This is interesting because when you write tests you will have to make sure your test tests the fact that exceptions are being thrown when expected and not being thrown when not expected.
Calculator.CalculatorTest class inside test/Calculator/CalculatorTest.java is empty. Lets talk about how to start writing this class.
In order to be able to write tests, you need to import a bunch of methods called assertions from a library called Junit. Here are some example assert methods -
assertEquals: takes two arguements, and checks if they are equal. If unequal, the test fails.
assertThrows: takes two arguements, the first being a class, and the second being a lambda expression.
You can get the class of an exception by using Exception.class, for example ArithmeticException.class or java.lang.ArithmeticException.class if youve not imported it.
The lambda expression should include the method call that should throw an exception. You can think of it as a small convinient try block, for example -
*****************************************************************************************
assertThrows(ArithmeticException.class,() ->
c1.divide());
assertThrows(ArithmeticException.class,() -> {
calculator c1 = new Calculator (1,2);
c1.divide();
}};
********************************************
Check this page for documentation on all assertions. You might be able to find something that reduces the amount of code you need to write!
We are using Junit 5, and the way you import these assertions for Junit 5 is -
import static org.junit.jupiter.api.Assertions.*;
Note: Make sure youve added package Calculator; to the top of the Calculator.CalculatorTest class file.
Every method that is meant to be a test needs to marked with a @Test decorator. This helps our tool find and identity which methods are intended to be tests. You can import this decorator by saying -
import org.junit.jupiter.api.Test;
Later, you can mark a method as a test by saying -
@Test
public void testAdd() {
}
*******************************************
You might also want to import java.lang.ArithmeticException, that is the exception thrown by the divide method.
Next, create a class as usual - public class CalculatorTest {}
Inside this class, define a bunch of calculator objects. Here is some, you might find useful -
public class CalculatorTest {
Calculator c1 = new Calculator(2.3);
Calculator c2 = new Calculator(-2.3);
Calculator c3 = new Calculator(2.-3);
Calculator c4 = new Calculator(-2.-3);
Calculator c5 = new Calculator(0.0);
Calculator c6 = new Calculator(0.3);
Calculator c7= new Calculator(2.0);
Calculator c8 = new Calculator(0.-3);
Calculator c9 = new Calculator(-2.0);
}
Next, use the @Test decorator just like how it was described about to create -
testAdd()
testSubstract()
testMultiply()
testDivide()
Implement all the above tests, and run them to make sure theyre all passing.
Once your tests are passing, hit check answer to run autograding - autgrading will try to run mutation tests as discussed in-class.
Make sure you modify your tests or source code to get rid of all surviving mutations. Once you are done, hit submit.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started