Question: This lab exercise will get you familiar with following: JUnit, a unit testing framework for Java programming language. Eclipse Debugger, Eclipse as a built-in debug
This lab exercise will get you familiar with following:
JUnit, a unit testing framework for Java programming language.
Eclipse Debugger, Eclipse as a built-in debug feature with its own perspective to help you resolve bugs in your software .
1 Preparation
Begin by starting a new project in Eclipse. Name your project DebugLab. Next, download the following files to your src folder inside the DebugLab folder.
Rational.java
RationalTester.java
Import JUnit library to your project by: Right click on the eclipse project and navigate:
Properties Java Build Path Libraries Add Library JUnit then hit Next and finish.
2 Testing with JUnit
We also provided a simple test suite RationalTester.java for you. To run the test suite in Eclipse, you can do one of the following:
In the Eclipse Menu bar, select Run Run As JUnit Test.
Right-Click on the project or Test Suite in the Package Explorer View and select Run As JUnit Test. Click on the green button with an arrow in the Eclipse toolbar below the Menu bar. Note: The Run Button will run the previously ran configuration, but should prompt for a run configuration if none was previously stated and multiple run types for the project exist.
The assertion TestRationalAdd should fail. The implementation for add method contains a bug. To fix this bug, you have to debug the code and fix the bug so that the new assertion passes (and all previous assertions do not regress).
3 Debugging
** In the picture:
public void tearDown() {
When debugging it may be helpful to know the line number of the code that you are viewing. Enable line numbers by selecting Window Preferences from the Menu Bar, expand General Editors in the Preference dialog, highlight Text Editors, and check the box Show line numbers.
Double click on TestRationalAdd and you should be pointed to the line where the assertion failed. In the Failure Trace, you can see that we expected a value of 0.25 to be returned from add method but it was 0.06. Set a breakpoint at the line where the assertion was failed so that we can debug the code and see what is misbehaving. To do this, in the Editor View double click on the gray line to the left of the line you want to set the breakpoint. A blue dot should appear like the image below. Now you can run the Debugger by one of the following method:
In the Eclipse Menu bar, select Run Debug As JUnit Test.
Right-Click on the project or Test Suite in the Package Explorer View and select Debug As JUnit
Test.
Click on the green bug button in the Eclipse toolbar below the Menu bar. Note: The Debug Button will run the previously ran configuration, but should prompt for a debug configuration if none was previously stated and multiple debug types for the project exist.
Eclipse should prompt to change to Debug Perspective. Click Yes to change the perspective. The program will now stop at any breakpoint you have set and allow you to view the current status of the program and step through your code. At this point you can view the variables at the current breakpoint and do any of the following:
Step Into (F5), will step into the current method call.
Step Over (F6), will step over the current line.
Step Return (F7), will continue stepping until the current method returns
At this point we want to step into the method call add to see what is going wrong. In this method we are adding the caller object rational number to the one passed as a parameter. A common denominator is found by multiplying the individual denominators.The new numerator for the addition will be (the first fraction numerator multiplied by the second denominator) added to (the first fraction denominator multiplied by the second fraction numerator) . From the Variables View, note the values of the following variables:
num
den
result
While your are tracing, consider the following questions:
Is the new numerator value calculated correct?
Is the new denominator value calculated correct?
Correct the bug you found then save. You can resume the code by clicking the green arrow in the Debug View toolbar, or pressing F9 or terminate the debugger by clicking the red box in the Debug View toolbar. Rerun the tests and ensure that you pass all of the test cases.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
