Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Here is the list of files in the Calculator Package - . src Calculator Calculator.java test Calculator CalculatorTest.java Calculator.Calculator class is fully implemented and contains
Here is the list of files in the Calculator Package
src
Calculator
Calculator.java
test
Calculator
CalculatorTest.java
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 testCalculatorCalculatorTestjava 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
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