Using JUnit With Eclipse IDE
Pages: 1, 2
- Create an instance of
junit.framework.TestCase. - Define tests that return void and whose name begins with the string "test" (such as
testWasTransactionSuccessful(),testShow(), etc.).
TestThatWeGetHelloWorldPrompt.java meets both of these criteria: it subclasses TestCase and has a method called testSay(). This method uses an assertEquals() call, which compares the value which we expect to receive against the value returned by say().
The main() method is used to run the tests and present their
output. JUnit's TestRunner executes our tests, and provides both graphical
and textual output. We use the textual version because that's what Eclipse IDE supports
and it's absolutely suitable for us. Once executed, the textual version of the test
shows the result as text output and Eclipse IDE uses that to create its own graphic presentation.
So, according TDD provisions, once we run our test we should see that it failed. Let's try. Click Run -> Run as -> JUnit Test (remember that TestThatWeGetHelloWorldPrompt.java should be highlighted in Package Explorer). In the left window, instead of Package Explorer, you will see the JUnit window, which shows a red bar, the failed tests, and details of those failures, as seen in Figure 2. If you do not automatically see it, just click on JUnit label (on the bottom left), which is one of the layers of this screen.

Figure 2. Failed test in JUnit
Perfect! It really fails. Now we can create working code in our project: right-click the
ProjectWithJUnit title in the left Package Explorer window, then choose New -> Class.
Choose a name for the class — we've assumed it to be HelloWorld. Do not check off
any of checkboxes on the bottom of the dialog window, just click Finish. Below is the code for
HelloWorld.java:
public class HelloWorld {
public String say() {
return("Hello World!");
}
}
It's very simple and merits no commentary. So let's test it and see the results. Run our test the same way as described above, and in the left JUnit window you will see a green bar, as seen in Figure 3. The green bar means that our test was successful.

Figure 3. Successful test in JUnit
Now we want to try to make it fail once again, but for a different reason. This will help show how
JUnit test covers and reports different errors. Edit the assertEquals()
to change the expected return value from "Hello World!" to, for example, "Hello Me!". When you run this
JUnit test again, the bar will be red, and at the bottom of the left JUnit window you will
see an explanation of what failed, as illustrated by Figure 4.

Figure 4. ComparisonError running JUnit
In conclusion, I would like to mention a few thoughts about testing as a necessary part of the development process. Testing code was always an integral part of any development. But it has been advanced over the last few years, thanks to powerful methodologies (such as "expectations-based development," etc.), accelerated testing toolkits, and the development of better testing processes. If you have found this article interesting, take some time to study formal testing methodology, and use it in your work.
Alexander Prohorenko is a certified professional, who holds Sun Certified System Administrator and Sun Certified Java Programmer certifications.
Olexiy Prokhorenko is a Sun Certified Enterprise Architect whose areas of interests include Web software architecture and development of software with frequently changing requirements.
Return to ONJava.com.
-
review
2009-04-25 02:33:35 ayushp [View]
-
I don't want JUnit in my production code!
2009-04-08 12:11:26 Alex_SF [View]
-
Excellent tutorial
2008-05-01 06:12:33 Tulasirm [View]
-
Thanks
2008-02-13 19:41:10 GraeC [View]
-
Do you have an exaple of TestSuites
2007-12-03 14:03:53 mugsi [View]
-
How do I run the same project from command line?
2007-11-19 21:18:49 connossieur [View]
-
helped a lot
2007-09-16 23:54:31 java_sushil [View]
-
junit.framework.AssertionFailedError: No tests found in package name.classname
2007-11-06 22:42:14 ajay_maheshwari [View]
-
Great Tutorial
2007-07-31 00:51:01 avnred [View]
-
Great Tutorial
2008-02-29 11:55:18 djor [View]
-
Great Tutorial
2008-08-29 04:39:11 Junit with eclipse [View]
-
Great Tutorial
2008-02-29 11:55:11 djor [View]
-
At Last! A Comprehensive tutorial
2007-07-27 08:22:06 StephaneLeFou [View]
-
not able to write the test case before coding the method to be tested
2007-03-10 01:55:20 ArchanaS [View]
-
V nice tutorial, Thanks a lot
2007-01-25 03:08:30 shadkam [View]
-
Marvellous,Nice,.....
2007-05-15 00:38:07 selflearner [View]
-
Hi
2006-11-26 21:18:18 laksm [View]
-
Excellent tutorial
2006-11-19 23:32:27 utpramod [View]
-
Nice Intro of JUnit and Eclipse
2006-09-23 04:40:01 Amp [View]
-
simple and excellent
2006-09-04 05:55:42 ucoreddy [View]
-
simple and excellent
2006-09-04 05:52:12 ucoreddy [View]
-
how to test a class
2006-08-28 23:54:50 junittesting [View]
-
It's Very good Tutorial
2006-08-14 00:14:42 NareshCheedella [View]
-
It's Very good Tutorial
2006-08-14 02:45:07 balagive [View]
-
great introduction to junit
2006-08-07 16:01:46 sasankmela [View]
-
Hi
2006-03-11 01:21:45 vdedaniya [View]
-
congrats Alexander Prohorenko i
2005-12-04 11:16:04 tdas10@yahoo.com [View]
-
Usung JUnit to test Struts application and JSP pages
2005-10-16 23:20:47 SahaPravi [View]
-
Usung JUnit to test Struts application and JSP pages
2006-02-23 21:20:34 Pradeepi [View]
-
Usung JUnit to test Struts application and JSP pages
2007-03-10 02:24:02 ArchanaS [View]
-
Using JUnit With Eclipse IDE
2005-08-17 00:38:59 jusloh [View]
-
Using JUnit With Eclipse IDE
2006-10-25 15:18:50 postingtalkbacks [View]
-
One Issue with the article
2005-07-27 17:10:42 asad_01 [View]
- Trackback from http://jay.shao.org/links/week_2004_05_16.html#000338
Trackback Message
2004-05-17 21:20:49 [View]
-
Check out this viewlet for TDD in Eclipse
2004-03-25 19:15:34 ceperez [View]
-
Check out this viewlet for TDD in Eclipse
2004-12-06 11:48:51 ceperez [View]
-
getting error message running JUnit
2004-03-12 04:18:39 cathryn [View]
-
getting error message running JUnit
2004-03-12 04:40:20 gr33n [View]
-
Eclipse
2004-02-23 01:22:28 gr33n [View]
-
Eclipse
2004-12-28 03:48:38 junit [View]
-
Eclipse
2005-05-31 00:15:45 Junit_Eclipse_Jan [View]
-
Eclipse
2004-03-10 10:03:42 pvocong [View]
-
junit naming conventions
2004-02-21 02:15:19 sundling [View]
-
I can't create testcase without a class for testing.
2004-02-20 23:37:45 oragon [View]
-
I can't create testcase without a class for testing.
2005-02-15 20:40:40 shyamsundarm.g [View]
-
I can't create testcase without a class for testing.
2004-02-25 02:09:46 kondareddy [View]
-
not bad:)
2004-02-09 08:31:34 totodo [View]
-
Hi
2006-11-26 21:15:58 laksm [View]