ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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

Updated on August 8, 2014

What is JUnit?

JUnit is a unit testing framework for JAVA whose support is built-in to ECLIPSE. When implemented as part of a JAVA project in ECLIPSE it provides a fully automated means of testing all methods.

The benefit of Junit testing can be enormous for the ECLIPSE JAVA developer. Once the JUnit tests are written the process can be totally automated. It is especially useful once an application is written and deployed. Code changes and enhancements can be verified for correctness. Also, before an application is written it can be used to help create the application by developing system tests before writing any code, a process known as test driven development (TDD). Automated testing and design methodology such as that which is found in using JUint is a key element in Agile programming, where requirements and code solutions proceed as an iterative and incremental process.



JUnit Versions and Differences

There are two versions of JUnit which are supported JUnit 3 and 4. As with all software a later version number indicates a JUnit 4 is the latest version available and includes some enhancements and advantages over JUnit 3. As such, JUnit 4 is the framework that will be used to incorporate JUnit in this tutorial.

If you have been following my series of tutorials we will be working with the project which is outlined in the tutorial entitled "ECLIPSE IDE - Creating "getters" and "setters" Eliminating Tedious Programming Tasks".

Creating a New Source Folder for the Test Cases

The JUnit tests will be created in the workspace and project that you are working on. Since the test cases are not really useful for an end user of our project, it makes sense that we create the test cases in a separate folder. In that way they can be easily eliminated from the deployable version of the project.

To do this you select your project. The follow File > New > Source Folder, then enter a folder name (I've chose test) and click on finish. The following snapshot indicates the location of the folder.

N.B. Note "Source Folder" and not "Folder".


Project Explorer Outline After test Folder Created

We created a new package for the test cases.
We created a new package for the test cases.

Setting Up the test Folder

The test classes should be created within the same packages as the classes being tested. Thus, we need to create the test package under the test folder with the exact same name as the package where the application classes were created. In other words the test folder will mirrors the application folder. Since the name org.setterand getters tutorial was specified.

To do this select the test folder, then choose New > Package.


Create a Package in the test Folder

Check that the test folder is correctly specified in the source folder name field.
Check that the test folder is correctly specified in the source folder name field.

Creating the First JUnit Test Case

To create the first test select the package that has just been created under the test folder. Then step through New > JUnit > JUnit Test Case. The class which we previously created was called BillingAddess. The standard practice for naming the test is to use the class name from the package followed by the word Test.

Earlier versions of Eclipse were not as explicit as the Kepler version and would give warning messages with respect to build class inclusion of the JUnit library. If ignored when creating test case resulted in unresolved references. If you are working with one of these earlier versions of Eclipse make sure that you specify a selection of either JUnit 3 or 4. Build path errors are easily corrected and we will take up the topic of build paths in a later turtorial. For now, its sufficient to know that the build path is a way of specifying the location of external libraries which are not part of the ECLIPSE framework. JUint is an add on and must be selected for inclusion in the build path.

Screens Presented During Test Class Creation

Note that the bottom part of this panel is a pop-up window asking whether JUnit 4 should be added to the build path.
Note that the bottom part of this panel is a pop-up window asking whether JUnit 4 should be added to the build path.

Choosing Methods to Test

ECLIPSE and JUnit give you the option to select the entire class or selected members to test. In this tutorial we selected the constructor method and get setCity and setState methods for examples. These methods are selected (checked). We then click on Finish and ECLIPSE has created the test class with stubs for the methods we selected This is illustrated in the following two snapshots.

Completing the Selection for the JUnit Test

Notice that the methods are listed for both the BillingAddress class as well as the inherited methods from the JAVA object class.
Notice that the methods are listed for both the BillingAddress class as well as the inherited methods from the JAVA object class.

The Code Generated by ECLIPSE Based on the Selections

Note that method stubs have been generated for the constructor,  setCity, and setState.
Note that method stubs have been generated for the constructor, setCity, and setState.

Analysis of the Generated Test Case Code

The following is a line by line analysis of the code.

Line 1: package org.GetterandSetter.tutorial;

As noted previously, the test case must be in the same package as the class to be tested. This statement does that.

Line 3: import static org.junit.Assert.*;

This makes the JUnit methods available to the program. The + sign in the margin shows that this expands to the two statements:

import static org.junit.Assert.*;

import org.junit.Test;

Line 5: public class BillingAddressTest {

The beginning of the test class.

Line 7: @Test

The @Test annotation is needed in intended identify methods intended to function as tests. This is a JUnit 4 newly defined requirement it supercedes JUnit 3 requirements regarding how tests were named. and the extends test case (i.e. creating the test case as a sub class of the framework class.

Line 8 : public void testBillingAddress() {

The beginning of the declaration for the constructor test.

Line 9: fail("Not yet implemented");

It should be obvious that there is some work needed here just by the statement wording. We'll be covering this in the next tutorial.



Running the Generated Code

The code isn't ready to succeed. There is work ahead for us! But, let's jst see what happen when we run this stub program. To do that we choose fro the menu Run > Run As > JUnit Test.

The snapshot which follows shows the results of running the test in the left had view of the ECLIPSE window. The results are as expect, the code had the fail assertion for each of the three methods. The bottom of the panel displays the method "Not yet implemented".


Results of the JUnit Test

The panel show the number of test runs. Indicates the failures and displays the message from the program.
The panel show the number of test runs. Indicates the failures and displays the message from the program.

What's Up Next

This tutorial introduced the JUnit feature which is built-in to ECLIPSE.

We stepped through creating a folder for the unit test for the BillingAddress class introduced in a previous tutorial.

An explanation was given as to why the test code should be "segregated" from the code to be deployed.

Naming conventions for the test class was given (although in actuality this was a JUnit 3 requirement which does not exist in JUnit 4).

Methods were selected to be included in the test class.

A line by line analysis was done for the generated code.

Running the test was demonstrated.

Results were presented.

In the next tutorial, we will look at writing the test class method code. I will include as an appendix to the next tutorial the BillingAddress class code so that those just beginning with this tutorial series will be on a firm footing.


JUnit Quiz - Did you make the Grade?

view quiz statistics

Was this tutorial helpful?

Cast your vote for Did this tutorial meet your expectations?
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)