Question: I need help on the second part 1-6 This is the 1st part. Creating and Running a JUnit Test First, in your lab3 package, create

I need help on the second part 1-6

This is the 1st part.

Creating and Running a JUnit Test

First, in your lab3 package, create a class Basketball with just the method stubs:

public class Basketball { public Basketball(double givenDiameter) { } public boolean isDribbleable() { return false; } public double getDiameter() { return 0; } public double getCircumference() { return 0; } public void inflate() { } } 

Creating a JUnit test in an Eclipse project

Eclipse makes this easy for us.

  1. In your lab3 package, create a new class called BasketballTests.
  2. Add JUnit to the build path, just as you have done for running a specchecker. (Right click on the project, select Build Path, and choose Add Libraries. Select JUnit and click Next. Choose JUnit 5 from the drop-down list and click Finish.)
  3. Add the line import org.junit.Test; after the package statement at the top of the file.
  4. Add the line import static org.junit.Assert.*;.
  5. Now you can start writing test cases. Each test case is just a public void method, with no parameters, that you have annotated with @Test.

For now, just copy and paste the code from BasketballTests on the previous page.

Running a JUnit test

Right-click on the BasketballTests class and select Run As --> JUnit Test. That's it. You'll normally see a new pane appear on the left side of the workspace labeled "JUnit":

The red colored bar indicates that some test cases have failed. In the list underneath it, tests that failed have an "X" in a blue circle, and those that succeeded have a check mark in a green circle. If you click on any of the test cases in the list, the "Failure Trace" pane below shows you which assertion failed along with its expected and actual values. Double-clicking on the assertion message will take you to the test case that failed.

I need help with this second part

You can re-run the entire set of tests by right-clicking on the class name in the JUnit pane and selecting Run. You can also do the same for any individual test case in the list.

  1. Add instance variables to the basketball class representing the diameter and inflated status. Initialize them in the constructor.
  2. Re-run the unit tests. Are more of the tests passing now?
  3. Fix the isDribbleable method.
  4. Re-run the unit tests. Are more of the tests passing now?
  5. Fix the inflate() method.
  6. Re-run the unit tests. Is everything passing now? When all the tests pass, the horizontal bar should be green instead of red. Victory!

import org.junit.Test;

import static org.junit.Assert.*;

public class BasketballTests

{

// margin of error for floating-point comparisons

private static final double EPSILON = 10e-07;

@Test

public void testInitial()

{

Basketball b = new Basketball(5);

assertEquals(false, b.isDribbleable());

}

@Test

public void testInitialDiameter()

{

Basketball b = new Basketball(5);

assertEquals(5.0, b.getDiameter(), EPSILON);

}

@Test

public void testInflate()

{

Basketball b = new Basketball(5);

b.inflate();

assertEquals(true, b.isDribbleable());

}

@Test

public void testDiameterAfterInflation()

{

Basketball b = new Basketball(5);

b.inflate();

assertEquals(5.0, b.getDiameter(), EPSILON);

}

}

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!