ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

ECLIPSE IDE - JUnit Testing a Key Element Agile Software Development for JAVA (Part II)

Updated on August 8, 2014

A Recap of Where Tutorial Part I Ended

Our last tutorial ended with the outline of a JUnit test program having been built and the JUnit test run. Naturally as we indicated at the end of the tutorial all of the test faild because the stub program which created the tests was merely a stub and each of the three method in the JUnit test had an assert statement which presented the output message, "Not implemented yet!"

In this tutorial we will take up the the tutorial where we left off and start coding which will test the three methods:

  • the testBillingAddress constructor
  • the testSetCity method
  • the testZipCode method

As previously mentioned, the code for the BillingAddress class is located in the Appendix section at the end of the tutorial.

So let's begin!


The JUnit Test Case Status at the End of the Previous Tutorial

The "stub" code for the JUnit test program at the end of the previous tutorial.
The "stub" code for the JUnit test program at the end of the previous tutorial.

Preliminaries and Creating a test for the First Method

If you have been following along and coding when you restart ECLIPSE you should see the test code displayed as indicated in the preceding snapshot. If you have been exploring features on your own, the window may appear differently. In that case, go to Window>Open Perspecitive > Java Perspective. If necessary, use the Project Explorer to open the test program.

Now we will begin coding the BillingAddress constructor test method. So let's see what the constructor was supposed to do.

// constructor

public BillingAddress() {
companyName = "blank";
streetAddress = "blank";
city = "blank";
state = "blank";
zipCode = 00000;

The constructor has 5 fields. Four of them are set to the word blank, and the fifth, the zipCode is set to zeros.

So, our steps are as follows:

  • Create a new BillingAddress object
  • Test that the fields of the object are set correctly.

The first line we have seen before:

BillingAddress ba1 = new BillingAddress();

The next line is a new one. The assertEquals method is a new one. It is part of the JUnit test package. The values provided to the method are expected value and the actual value. In this case the expected value, the value set in the constructor was the word blank.For the actual value we obtain it by utilizing the getBillingAddress method with respect to the object ba1.

To reiterate this point, the template for assertEquals is:

assertEquals(expected value, actual value);

and our statement becomes:


This statement is repeated for the next three fields of the BillingAddress, ba1, substituting the appropriate "getter" for each.

The last statement of the test case is slightly different in that the field zipCode is an integer.

The Code for the BillingAddress Constructor

The code for the BillingAddress constructor method.
The code for the BillingAddress constructor method.

Further Explanation of the assertEquals Mehod

There is a difference between the assertEquals and method we have observed thus far.

In the case of the getCity method we had to do the following

  • we had to create an object of the type BillingAddress, and
  • refer to the method quaified by the created opject name as ba1.getCity();.

Why don't we have?

BillingAddressTest bat = new BillingAddressTest();


This is because assertEquals is a static method.

A static method is:

  • a method which belongs to the class as a whole.
  • doesn't belong to an instance of the class, doesn't belong to an object
  • the syntax is class.method.
  • if we are using it in the class it belongs to the class prefix can be omitted.

Note also, ECLIPSE assists the developer by displaying the text of a static method in italics.

To better see this hover over one of the assert statements and see what ECLIPSE know the qualification of the method is. (See the following snapshot.)

The assertEquals Static Method

Hover over the assertEquals text. ECLIPSE displays it's full qualification.
Hover over the assertEquals text. ECLIPSE displays it's full qualification.

Run the JUnit Test

The BillingAddressTest can now be run for the BillingAddress constructor. The following snapshot illustrates the result. The test passed. The remaining two tests case for the setCity and setState will be completed next.

The Test Results Thus Far

Our first test runs. The other two not implemented yet fails
Our first test runs. The other two not implemented yet fails

Completing the Test Cases

We complete the test cases for setCity and setState each in a similar fashion. In each case, we create a BillingAddress object and then call the assertEquals method with the the value each was set with for the expected and invoke the appropriate get method to retrieve the actual.value.

The Code for the Remaining Tests

The code for the two remaining test cases.
The code for the two remaining test cases.

Rerunning the Tests

We now rerun the tests and, Success! Note the green bar replaces the bark colored bar which was displayed when there were failures.

Successful Test Results Display

Success! Note the green bar.
Success! Note the green bar.

Looking at a Test Failure Output

Suppose something went wrong with a test, what information could we get to help us solve the problem?

Let's create an error in the second test. Make a typo in the expected value for "Scranton" by spelling it "Scrantno" and see what happens.

From the test results view, highlight the second test, the one which failed and maximize the failure trace part of the JUnit test results view. The text reports what is wrong. There are two icons in the upper right hand corner of this part of the view which show alternative ways of look at the error. These two ways are illustrated in the snapshots below.

Two Ways of Viewing the Failure Trace

This is the default view of the failure trace.
This is the default view of the failure trace.
This is an alternate view seen by clicking the far right icon in the failure trace section.
This is an alternate view seen by clicking the far right icon in the failure trace section.

Recap and What's Next

In this tutorial we completed the test case for the methods we selected when we created the "stub" test program in the previous tutorial. The fail method (which is also a static method of the JUnit software is replaced by appropriate get and set methods as the test case may require. We ran the tests and noted the success of the tests once appropriate code was added. We also looked at two ways to look at the output of a failure in a test.

In the tutorial we just completed, the methods to be tested had already been built. In the next tutorial we will look at the case of specifying the test and then creating the method which will produce the desired output. This will be our first look at the "test first" approach and our introduction to Test Driven Development (TDD).

Did This Tutorial Meet Your Needs and Expectations?

Cast your vote for ECLIPSE IDE Tutorial


    0 of 8192 characters used
    Post Comment

    No comments yet.


    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, 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:

    Show Details
    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 or domains, for performance and efficiency reasons. (Privacy Policy)
    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)
    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.
    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)