Question: 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 JUnit5 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 n-th harmonic number, which is calculated like this: 1/1+1/2+1/3+...+1/n.
If n is negative, the method should return the additive inverse of the n-th 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 floating-point numbers
Because of the way floating-point numbers are represented in Java, many numbers cannot be represented with full mathematical precision (e.g.1/3) To account for that fact, JUnit provides an overloaded assertEquals methodLinks to an external site. that asserts that the expected and actual floating-point 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 JUnit5, 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:
assertThrows(IllegalArgumentException.class, ()->{
// 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!