Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need to write this all in a test file named HarmonicTest.java I need to write 5 JUnit5 tests for an existing method I have

I need to write this all in a test file named HarmonicTest.java

I need to write 5 JUnit5 tests for an existing method I have which looks like the following:

public static double harmonic(int n) {

if (n == 0)

throw new IllegalArgumentException("The argument n can't be zero.");

if (n == 1)

return 1;

else if (n < 0)

return -1 * harmonic(-n);

else // n > 1

return 1.0 / n + harmonic(n - 1);

}

These

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 method (Links 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

So the answer I am looking for is the HarmonicTest.java file with 5 JUnit tests

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

Data And Information Quality Dimensions, Principles And Techniques

Authors: Carlo Batini, Monica Scannapieco

1st Edition

3319241060, 9783319241067

More Books

Students also viewed these Databases questions

Question

What is the purpose of awarding damages for breach of contract?

Answered: 1 week ago

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago