Answered step by step
Verified Expert Solution
Link Copied!

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 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

The Database Management Systems

Authors: Patricia Ward, George A Dafoulas

1st Edition

1844804526, 978-1844804528

More Books

Students also viewed these Databases questions

Question

4. Devise an interview strategy from the interviewers point of view

Answered: 1 week ago