Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In this second part of the assignment, you will write JUnit 5 tests for an existing method. Add a fourth static method to the class
In this second part of the assignment, you will write JUnit tests for an existing method.
Add a fourth static method to the class Recursion. Its name is harmonic, it has one parameter n of type int, and the return type is double.
The method is supposed to do the following:
If n is positive, the method should return the nth harmonic number, which is calculated like this: n
If n is negative, the method should return the additive inverse of the nth harmonic number.
If n is zero, an IllegalArgumentException should be thrown.
Rather than implementing the method, copy this implementation into your code.
If you need to adjust the indentation, go to Eclipse Source Format. It might be able to fix the indentation for you.
Add a JUnit test file called HarmoicTest to the source folder called test.
Write at least five different JUnit test methods to test the method harmonic using both valid and invalid input.
Choose the tests deliberately to provide thorough testing that uncovers as many potential problems as possible.
Each of the five JUnit test methods should have a descriptive name that indicates what it is testing for.
Two things to consider:
How to compare floatingpoint numbers
Because of the way floatingpoint numbers are represented in Java, many numbers cannot be represented with full mathematical precision eg To account for that fact, JUnit provides an overloaded assertEquals methodLinks to an external site. that asserts that the expected and actual floatingpoint numbers are equal within a given delta.
Use this overloaded method to test the method harmonic.
To ensure consistency throughout your test class, declare a private final field called DELTA and assign it one billionth.
How to test for exceptions:
In JUnit we use lambda expressions to test for exceptions. We'll cover lambda expressions later in the course. All you need to know for now is this special case which has the following format:
assertThrowsIllegalArgumentExceptionclass,
call here the method to trigger the exception
;
Notice, that the code snippet above calls the static method assertThrows from class Assertions without explicitly calling it on the type. That works if you included the following static import statement, which was also used in the other three JUnit test classes.
import static org.junit.jupiter.api.Assertions.;
Here is a resource with more information and examplesLinks to an external site. of testing exceptions. Note that the resource calls assertThrows on the type, assuming that no static import statement is used
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