Eclipse and JUnit
In this lab, you will explore Eclipse. If you already use the Eclipse
editor, this lab, may provide you with more information.
All Eclipse applications are built as a project. Each lab and
each PA that you do this semester should be in its own project and
each project will have its own folder.
Part 1 - Hello World
- If you have not done so already, open Eclipse. At the "Workspace"
prompt, fill in your workspace. This could be a folder on a thumb
drive or your desktop.
- Choose the tutorials icon. (Pen and green checkbox).
- Choose "Create a Hello World application".
- Follow the steps in the tutorial. Be sure to see how the
automatic syntax checking works by introducing some bugs. Also explore
the autofill options (showing the list of available members of an
object).
Part 2 - Importing Preferences
- Download
CS159_formatter_V1.xml
from the course Supplemental Material page.
- Follow the instructions on that page to import the file into
Eclipse.
- Test the auto-format feature by selecting some code in your Hello
World project and pressing Shift+Ctrl+F. You can also find the auto-format option under the Source menu.
Part 3 - Import other .java files into a project
Next, you will practice downloading existing java source files and importing them into your project.
- Download the files
Point.java, PointUtils.java,
and PointDemo.java to your
Desktop.
-
Drag those file into the "src" section of your project.
At this point you should see some red x's indicating that there are errors in your newly imported files. We'll deal with that in the next section.
Note that you can also import .java files using the "File" menu.
You should NEVER mess with files in your Eclipse workspace using an
external file manager. Always add or remove files through the Eclipse
interface.
- Take a few minutes to figure out what
PointDemo.java
is
supposed to accomplish. At this point the
class PointDisplay
is missing, but you can see its
javadoc here: PointDisplay.html.
Part 4 - Adding .class files to a project
Sometimes it may be necessary to include a pre-built java .class file in your project.
- Download the files
PointDisplay.class and PointDisplay$PointCanvas.class to your
Desktop.
- Go to "Project / Properties" and click on "Java Build Path".
- Click on "Libraries" -> "Add Class Folder" -> "New Folder". Give your folder a name such as "classes".
- Press "Okay". You will see a new section in the Package Explorer window called "Referenced Libraries" and you should see your new folder beneath it.
- In the Eclipse window, drag and drop your .class files into the folder you just created. All red x's should disappear.
- Run
PointDemo.java
to confirm that everything is working as expected.
Part 5 - JUnit testing
JUnit testing is supported by Eclipse. For this part of the lab,
we'll use Eclipse to build JUnit tests for for the PointUtils class.
This class does contain at least one error. Don't fix any
errors. Your goal for this stage of the lab is to write JUnit
tests that catch errors in the existing code.
- To create a new JUnit test for a particular file in the project, right click on the name of the file in the "src" list and choose "New / JUnit Test Case".
- Click on the "JUnit 4" Test radio button.
- The name of the file defaults to the name of the active class with Test appended (PointUtilTest).
- Click on "Generate comments", and then click on "Next".
- From the list of methods of the PointUtil class, click on the box beside each of the two methods in the class. Don't worry about the Object class below it.
- Click "Finish".
- Implement JUnit tests for the two methods in PointUtil. Adequate testing will probably require more than just two test methods.
- Run your tests by right-clicking PointUtilsTest and selecting "Run as" -> "JUnit Test".
- If your tests are implemented correctly, at least one should fail. Try fixing
PointUtils.java
to confirm that it passes your tests after any errors are corrected.
- Submit your completed
PointUtilsTest.java
through
Canvas. Your grade on this lab will be based on this submission.
Part 6 - CodingBat
If you have not already done so (last semester) go to CodingBat.com
and set up an account. Once you have an account, you can add your
instructor by going to Prefs and then adding your
teacher's e-mail address (spragunr@jmu.edu). If you already have a
previous teacher listed, you can remove them now.
Do the problem in Warm-Up 2 called frontTimes
(http://codingbat.com/prob/p101475).
This is a modified version of a lab designed by Michael Norton and Nancy Harris.