ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

ECLIPSE IDE - Tutorial Adding a Class to the Package After Creating the Test

Updated on August 8, 2014

Creating a New Class for Our Car Rental Agency

Chipmunk had decided to start a car rental agency. He went around town and created a relationship with a number of potential clients. Since he was eager to make some money, he started out creating his BillingAddress class first. He thought he would use it for advertising as well when he was ready to go public. He had enough interest from potential customers that he went out and bought lots of cars, but only one of each model. (This fact will become important later.)

Now, he needs to have a Car class added to his application. The developer he hired believes in the "test first" methodology and has proposed creating the test method, testCar, first. Chipmunk is skeptical but he said, "Go ahead!".

In this tutorial we'll see how it went.

Creating the CarTest JUnit Test

To create the CarTest JUnit test, one first selects the package in the project root folder and right clicks on New > JUnit test. When the menu for the new JUnit test appears we enter the name, CarTest. The "Class under test field:" is left blank as no class is associated with this test as of yet. .Click on Finish. and the new test case is created. These actions are illustrated in the following snapshots.


Creating the JUnit Test

We highlighted our package under the test folder then right clicked New followed by JUnit test. We leave the name of the class ot be tested blank.
We highlighted our package under the test folder then right clicked New followed by JUnit test. We leave the name of the class ot be tested blank.
Our test case "stub" class.
Our test case "stub" class.

Filling in the Test Case Details

The test case coding is rather simple. We create a new object of the Car class and create calls to the static method assertEquals for the car model, model year, and the name of the car manufacturer.

There are several things to note in this example. First, the constructor initializes the model field; and secondly, notice that the indicates all of the assignment statements are flagged with errors. Finally notice that in the assertEquals method calls we use field names than the get calls as used when we created the AddressBilling test class.


Running the Test

Although we have errors in the test, ECLIPSE will still allow it to be run. The test is run by selecting Run> JUnit Test. A warning popup appears, click on Proceed.

Not surprisingly, the test fails. If you look at the messages, ECLIPSE reported that "Car cannot be resolved to a type". This just means that there is no Car class that ECLIPSE know about, we have not created it yet.

Output of Failed Test

The popup window indicates that there is an error , but ECLIPSE allows us to proceed.
The popup window indicates that there is an error , but ECLIPSE allows us to proceed.
The resulting failure completion messages which give the indication that Car could not be resolved (it doesn't exist).
The resulting failure completion messages which give the indication that Car could not be resolved (it doesn't exist).

Fixing the Problem with Help from ECLIPSE

If you hover over the word car in the source. ECLIPSE has a number of suggestions for how to fix the proble. This "quick fix" assistance can help to resolve most problems although at time ECLIPSE is baffled as to what to do and will just report a message, "No suggestions".

In this case, we know that the problem is that the class does not exist, so we just double click an the option and the ECLIPSE window to create a new class opens automatically. When ECLIPSE proposes a "quick fix", sometimes there some sort of subtle problem with the proposed fix. In this case there is such a problem. ECLIPSE proposed creating a class (this is great) but the destination indicated is to create the class in the test folder. This is NOT what we want. We want the the class to be created in our src folder. Easily corrected, but it serves a good example that one should look over any "quick fix" suggestion content.

All we need to do is Browse and click on src. ECLIPSE then proceed to create the Car class "stub". as illustrated below.

ECLIPSE has Created the "Missing" Class

ECLIPSE has created the Car class stub.
ECLIPSE has created the Car class stub.

Return to the Car Test Program & Correct Errors

Looking at CarTest.java we see a number of errors. In each case we will let ECLIPSE "quick fix" help us to resolve them.

Hovering over the line:

Car c1 = new Car("sunfire");

ECLIPSE has two suggestions. The appropriate one is to create the constructor method, which ECLIPSE proceeds to do and opens the view to that source.

Not that if we do not perform a save on Car.java at this point when we return to CarTest.java, the error persists. Thus, when correcting errors always remember to perform a save for any suggested code change at that point. It helps to prevent any confusion. If the code change had a side effect resulting in an error you would know that the code change save had some adverse effect. Associating the suggested code change with the side effect might help you resolve the resulting problem.

The following three statements:

assertEquals("Sunfire",c1.model);
assertEquals(2006,c1.modelYear);
assertEquals("Pontiac",c1.manufacturer);

are resolved by hovering over them. The ECLIPSE "quick fix" suggestions differ, but the appropriate action of creating fields is the same. Note that the field type should be set to String or int ih these cases.

The snapshots which follow indicate our process through the error messages.

Correcting Code Problems - Statement by Statement

ECLIPSE initiates the New Class wind. Note that  a change had to be made in the destination folder, from the suggestion of "test" to "src".
ECLIPSE initiates the New Class wind. Note that a change had to be made in the destination folder, from the suggestion of "test" to "src".
Errors are reflected for CarTest. A constructor method and fields need to be added to the Car claas.
Errors are reflected for CarTest. A constructor method and fields need to be added to the Car claas.
ECLIPSE's suggestions, Choose the "Create constructor..." option.
ECLIPSE's suggestions, Choose the "Create constructor..." option.
The constructor "stub" as been created. Note the TODO comment. Later we will see how this comment is very helpful as a reminder of tasks to be performed.
The constructor "stub" as been created. Note the TODO comment. Later we will see how this comment is very helpful as a reminder of tasks to be performed.
Note the suggestion for creating the missing field. This option is selected and the field is created.
Note the suggestion for creating the missing field. This option is selected and the field is created.
ECLIPSE has an additional suggestion here. ECLIPSE thinks "model" might have been the intention., however creating a field is what is needed.
ECLIPSE has an additional suggestion here. ECLIPSE thinks "model" might have been the intention., however creating a field is what is needed.
The suggestions for "manufacturer". Again "Create field..." is appropriate.
The suggestions for "manufacturer". Again "Create field..." is appropriate.

The Car Class After Adding the Fields

The fields have been added and appropriate changes as to field type have been made.
The fields have been added and appropriate changes as to field type have been made.

The Completed Constructor Method

The information in the constructor method corresponds to the values expected in the CarTest.
The information in the constructor method corresponds to the values expected in the CarTest.

ECLIPSE Creates the "getters" and "setters"

The option to create "setters" and "getters" is made. There is no need to create the "setter" for the field "model" since model is the argument passed to the Car method.
The option to create "setters" and "getters" is made. There is no need to create the "setter" for the field "model" since model is the argument passed to the Car method.

Success!

The CarTest now runs successfully.
The CarTest now runs successfully.

Wrap Up and What's Next

This tutorial featured the "test first" method. A test case was developed before the class was written.

ECLIPSE we observed can do a great deal in helping in this "code on the go approach".After specifying our field requirements and expected results, ECLIPSE through its quick fix suggestions helped us move from a test case with no associated class. To the complete class definition and have a successful test run.

As for what's next. There are several minor points that it seem a good idea to defer discussion on which we will address in the next tutorial which will focus on the relationship between classes based on a common field.




Did this tutorial meet your needs and expectations?

5 out of 5 stars from 1 rating of Please Rate the Content of This Tutorial
working

This website uses cookies

As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://corp.maven.io/privacy-policy

Show Details
Necessary
HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
LoginThis is necessary to sign in to the HubPages Service.
Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
AkismetThis is used to detect comment spam. (Privacy Policy)
HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
Features
Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
MavenThis supports the Maven widget and search functionality. (Privacy Policy)
Marketing
Google AdSenseThis is an ad network. (Privacy Policy)
Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
Index ExchangeThis is an ad network. (Privacy Policy)
SovrnThis is an ad network. (Privacy Policy)
Facebook AdsThis is an ad network. (Privacy Policy)
Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
AppNexusThis is an ad network. (Privacy Policy)
OpenxThis is an ad network. (Privacy Policy)
Rubicon ProjectThis is an ad network. (Privacy Policy)
TripleLiftThis is an ad network. (Privacy Policy)
Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
Statistics
Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)
ClickscoThis is a data management platform studying reader behavior (Privacy Policy)