<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Wolverine</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Wolverine"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Wolverine"/>
	<updated>2026-05-09T05:54:51Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29749</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29749"/>
		<updated>2009-11-19T15:13:49Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Ways to Adapt Using Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
We will now discuss few important ones.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design === &lt;br /&gt;
According to the research published in ''[10]''&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;br /&gt;
&lt;br /&gt;
===Service Oriented Architecture ===&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
''Example of an SOA::''&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Meeting pattern Example===&lt;br /&gt;
According to the research published in [6].&amp;lt;br&amp;gt;&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
''Meeting pattern Example::''&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29748</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29748"/>
		<updated>2009-11-19T15:07:33Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design === &lt;br /&gt;
According to the research published in ''[10]''&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29747</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29747"/>
		<updated>2009-11-19T15:07:21Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Ways to Adapt Using Design Patterns */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design === &lt;br /&gt;
Accordin to the research published in ''[10]''&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29746</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29746"/>
		<updated>2009-11-19T15:06:26Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design [10] */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design === ''[10]''&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29745</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29745"/>
		<updated>2009-11-19T15:06:08Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design [10]===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29743</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29743"/>
		<updated>2009-11-19T15:04:29Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29740</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29740"/>
		<updated>2009-11-19T15:02:48Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&amp;lt;br&amp;gt;&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Characteristic of components in each combination::''&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''CPU Usage Comparison ::''&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
UPT is utilized processor time and TPT is total processor time&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;''Energy Usage Comparison::''&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29737</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29737"/>
		<updated>2009-11-19T14:55:41Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;Characteristic of components in each combination::&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&amp;lt;br&amp;gt;&lt;br /&gt;
CPU Usage Comparison ::&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&amp;lt;br&amp;gt;&lt;br /&gt;
Energy Usage Comparison::&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29734</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29734"/>
		<updated>2009-11-19T14:53:53Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()::''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
&amp;lt;br&amp;gt;For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&amp;lt;br&amp;gt;Characteristic of components in each combination&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&lt;br /&gt;
CPU Usage Comparison ::&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&lt;br /&gt;
Energy Usage Comparison::&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg|Energy Usage Comparison]]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29733</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29733"/>
		<updated>2009-11-19T14:51:16Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&amp;lt;br&amp;gt;&lt;br /&gt;
Lets look at both versions.&amp;lt;br&amp;gt;&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
Characteristic of components in each combination&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg]|Energy Usage Comparison]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29732</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29732"/>
		<updated>2009-11-19T14:43:37Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&lt;br /&gt;
Lets look at both versions.&lt;br /&gt;
Class Diagram of sample Factory Method pattern::&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
Sequence Diagram (with Activations) of sample Factory Method pattern :: &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
Characteristic of components in each combination&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg]|Energy Usage Comparison]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29731</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29731"/>
		<updated>2009-11-19T14:40:31Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&lt;br /&gt;
Lets look at both versions.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg|Class Diagram of sample Factory Method pattern ]]&lt;br /&gt;
 &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg|Sequence Diagram (with Activations) of sample Factory Method pattern ]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg|Class Diagram of Factory Method pattern]]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg|Characteristic of components in each combination ]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg|CPU Usage Comparison ]]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg]|Energy Usage Comparison]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29730</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29730"/>
		<updated>2009-11-19T14:39:31Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Energy Conscious Factory Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&lt;br /&gt;
Lets look at both versions.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]|Class Diagram of sample Factory Method pattern ]&lt;br /&gt;
 &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]|Sequence Diagram (with Activations) of sample Factory Method pattern ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg]|Class Diagram of Factory Method pattern]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg]|Class Diagram of Factory Method pattern]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]|Characteristic of components in each combination ]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]|CPU Usage Comparison ]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg]|Energy Usage Comparison]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig8.jpg&amp;diff=29729</id>
		<title>File:ECFactory Fig8.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig8.jpg&amp;diff=29729"/>
		<updated>2009-11-19T14:21:40Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29728</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29728"/>
		<updated>2009-11-19T14:21:19Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Working Notes */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg| General Strategies]]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&lt;br /&gt;
Lets look at both versions.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]|Class Diagram of sample Factory Method pattern ]&lt;br /&gt;
 &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]|Sequence Diagram (with Activations) of sample Factory Method pattern ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg]|Class Diagram of Factory Method pattern]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg]|Class Diagram of Factory Method pattern]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]|Characteristic of components in each combination ]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]|CPU Usage Comparison ]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg]|Energy Usage Comparison]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29727</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29727"/>
		<updated>2009-11-19T14:19:45Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ref: Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Some strategies and their experimental results are discussed in [10]:&lt;br /&gt;
[[Image:ECFactory_Fig8.jpg]| General Strategies]&lt;br /&gt;
&lt;br /&gt;
'''''Factory Method pattern''''' is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
Factory Method Pattern is a good pattern to build upon for energy conscious pattern.&lt;br /&gt;
Lets look at both versions.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]|Class Diagram of sample Factory Method pattern ]&lt;br /&gt;
 &lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]|Sequence Diagram (with Activations) of sample Factory Method pattern ]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
'''''Modified  Factory  Method  pattern''''' &lt;br /&gt;
As regular Factory Method pattern was not designed for being energy concerned, so, it is slightly modified. Some of the critical tweaks are:&lt;br /&gt;
*Factory Method Pattern that mixes usage of struct and class in several manners. &lt;br /&gt;
*Factory  Method  Pattern  that  uses  static  factory  method  in Factory struct &lt;br /&gt;
*Factory Method Pattern that applies generics principle which is parametered type &lt;br /&gt;
*Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg]|Class Diagram of Factory Method pattern]&lt;br /&gt;
&lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
''Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()''&lt;br /&gt;
[[Image:ECFactory_Fig4.jpg]|Class Diagram of Factory Method pattern]&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
'''''Experimental Result:'''''&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]|Characteristic of components in each combination ]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]|CPU Usage Comparison ]&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig7.jpg]|Energy Usage Comparison]&lt;br /&gt;
&lt;br /&gt;
It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;br /&gt;
So it is clearly proven how this pattern can save energy.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig7.jpg&amp;diff=29722</id>
		<title>File:ECFactory Fig7.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig7.jpg&amp;diff=29722"/>
		<updated>2009-11-19T14:00:49Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: from Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-5&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;from Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig4.jpg&amp;diff=29721</id>
		<title>File:ECFactory Fig4.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig4.jpg&amp;diff=29721"/>
		<updated>2009-11-19T14:00:28Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: from Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-5&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;from Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig3.jpg&amp;diff=29720</id>
		<title>File:ECFactory Fig3.jpg</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=File:ECFactory_Fig3.jpg&amp;diff=29720"/>
		<updated>2009-11-19T13:59:39Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: from Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-5&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;from Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29719</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29719"/>
		<updated>2009-11-19T13:57:30Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
11. http://www.codeproject.com/csharp/structs_in_csharp.asp &lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig3.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ref: Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : Energy Conscious design pattern and Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Energy Conscious Factory Design ===&lt;br /&gt;
&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  [7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Factory Method pattern is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
&lt;br /&gt;
Figure 1. Class Diagram of sample Factory Method pattern &lt;br /&gt;
 &lt;br /&gt;
Figure 2. Sequence Diagram (with Activations) of sample Factory Method pattern &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
Figure 3. Class Diagram of Factory Method pattern :  &lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()&lt;br /&gt;
Figure 4. Class Diagram of Factory Method pattern&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
Experimental Result:&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
 Characteristic of components in each combination &lt;br /&gt;
Fig5 &lt;br /&gt;
&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
fig6&lt;br /&gt;
&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
fig 7&lt;br /&gt;
&lt;br /&gt;
Conclusion(this can be put in the main conclusion) :: It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29718</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29718"/>
		<updated>2009-11-19T13:50:04Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9. Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10. Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ref: Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : &lt;br /&gt;
&lt;br /&gt;
   1. Energy Conscious design pattern&lt;br /&gt;
   2. Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
Energy Conscious Factory Design&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Factory Method pattern is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
&lt;br /&gt;
Figure 1. Class Diagram of sample Factory Method pattern &lt;br /&gt;
 &lt;br /&gt;
Figure 2. Sequence Diagram (with Activations) of sample Factory Method pattern &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
Figure 3. Class Diagram of Factory Method pattern :  &lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()&lt;br /&gt;
Figure 4. Class Diagram of Factory Method pattern&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
Experimental Result:&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
 Characteristic of components in each combination &lt;br /&gt;
Fig5 &lt;br /&gt;
&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
fig6&lt;br /&gt;
&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
fig 7&lt;br /&gt;
&lt;br /&gt;
Conclusion(this can be put in the main conclusion) :: It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29717</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29717"/>
		<updated>2009-11-19T13:49:29Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
8. WIM Workshop,Uppsala, 17.9.2004&lt;br /&gt;
&lt;br /&gt;
9.Mobile Application Development, Sam Nasr , www.CleavelandDotNet.info&lt;br /&gt;
&lt;br /&gt;
10.Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
   by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
&lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ref: Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : &lt;br /&gt;
&lt;br /&gt;
   1. Energy Conscious design pattern&lt;br /&gt;
   2. Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
Energy Conscious Factory Design&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Factory Method pattern is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
&lt;br /&gt;
Figure 1. Class Diagram of sample Factory Method pattern &lt;br /&gt;
 &lt;br /&gt;
Figure 2. Sequence Diagram (with Activations) of sample Factory Method pattern &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
Figure 3. Class Diagram of Factory Method pattern :  &lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()&lt;br /&gt;
Figure 4. Class Diagram of Factory Method pattern&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
Experimental Result:&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
 Characteristic of components in each combination &lt;br /&gt;
Fig5 &lt;br /&gt;
&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
fig6&lt;br /&gt;
&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
fig 7&lt;br /&gt;
&lt;br /&gt;
Conclusion(this can be put in the main conclusion) :: It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29705</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29705"/>
		<updated>2009-11-19T05:01:42Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* The Mobile Application Environment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[22 http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ref: Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : &lt;br /&gt;
&lt;br /&gt;
   1. Energy Conscious design pattern&lt;br /&gt;
   2. Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
Energy Conscious Factory Design&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Factory Method pattern is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
&lt;br /&gt;
Figure 1. Class Diagram of sample Factory Method pattern &lt;br /&gt;
 &lt;br /&gt;
Figure 2. Sequence Diagram (with Activations) of sample Factory Method pattern &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
Figure 3. Class Diagram of Factory Method pattern :  &lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()&lt;br /&gt;
Figure 4. Class Diagram of Factory Method pattern&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
Experimental Result:&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
 Characteristic of components in each combination &lt;br /&gt;
Fig5 &lt;br /&gt;
&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
fig6&lt;br /&gt;
&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
fig 7&lt;br /&gt;
&lt;br /&gt;
Conclusion(this can be put in the main conclusion) :: It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29693</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29693"/>
		<updated>2009-11-19T04:57:55Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* The Mobile Application Environment */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen on mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with[http://www.umsl.edu/~subramaniana/concepts1.html].  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems [http://en.wikipedia.org/wiki/Mobile_operating_system] on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
Service Oriented Architectures are well suited for the mobile environment [3].  Within this architecture, applications can make use of various services by sending messages to them via a protocol.  Applications may interact with services on operating systems and written in different languages, as long as the protocol is followed.  Services may be located on different mobile devices.  This protocol is a good fit, allowing applications to reach out to services on other mobile devices, even though they may be on very different devices.&lt;br /&gt;
&lt;br /&gt;
*'''''Service Oriented Architecture Example''''' [3]&lt;br /&gt;
&lt;br /&gt;
[[Image:SOA_Example.jpg]]&lt;br /&gt;
&lt;br /&gt;
The Energy Conscious (EC) Factory model [7]!!!!&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
*'''''Meeting pattern Example''''' [6]&lt;br /&gt;
&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html - Model View Controller Architecture&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge - Challenges of the Mobile Environment&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/ - Designing and Developing mobile web sites in the real world&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
== Working Notes ==&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Ref: Energy Conscious Factory Method Design Pattern for Mobile Devices with C# and Intermediate Language, &lt;br /&gt;
by Kayun Chantarasathaporn  and Chonawat Srisa-an, Ph.D.  at  Faculty of Information Technology,  Rangsit University, Thailand.&lt;br /&gt;
-----------------------------------&lt;br /&gt;
&lt;br /&gt;
Two major design patterns that have come up to help in mobile softwares and to address the above discussed issues in a mobile device are : &lt;br /&gt;
&lt;br /&gt;
   1. Energy Conscious design pattern&lt;br /&gt;
   2. Mobile agent design pattern&lt;br /&gt;
&lt;br /&gt;
Energy Conscious Factory Design&lt;br /&gt;
Power consumption has not been much of a concern to developers in the past, but this is important in the mobile environment.  Battery power on cell phones is limited.  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[7]&lt;br /&gt;
- Use registers instead of memory whenever possible (registers use less energy)&lt;br /&gt;
- Use structs instead of classes&lt;br /&gt;
- Use static instead of dynamic&lt;br /&gt;
&lt;br /&gt;
Factory Method pattern is a pattern that returns an object from one of various possible classes.  Which class is chosen to be an object generator  is  depended  on  data  provided  to  it.  &lt;br /&gt;
&lt;br /&gt;
Figure 1. Class Diagram of sample Factory Method pattern &lt;br /&gt;
 &lt;br /&gt;
Figure 2. Sequence Diagram (with Activations) of sample Factory Method pattern &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Modified  Factory  Method  pattern applies both struct  and  class recommendations.&lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Class, ConcreteCreator Struct with Non-Static FactoryMethod()&lt;br /&gt;
Figure 3. Class Diagram of Factory Method pattern :  &lt;br /&gt;
In  this  case,  we  use VehicleChooser  which  is  a  ConcreteCreator  as  struct  while  Car, Plane  and  Ship  which  are  ConcreteProduct  as  class.    Method ChooseVehicle(...) which is as FactoryMethod() is non-static. &lt;br /&gt;
&lt;br /&gt;
 Factory Method Pattern That Uses ConcreteProduct Struct, ConcreteCreator Struct with Static FactoryMethod()&lt;br /&gt;
Figure 4. Class Diagram of Factory Method pattern&lt;br /&gt;
If we use static method appropriately, CPU workload will be reduced.  This affects directly in lessening power consumption as a whole.&lt;br /&gt;
&lt;br /&gt;
Experimental Result:&lt;br /&gt;
For various combination shown in figure, the tests were run, and the result obtained are shown in the graph below.&lt;br /&gt;
 Characteristic of components in each combination &lt;br /&gt;
Fig5 &lt;br /&gt;
&lt;br /&gt;
CPU Usage Comparison &lt;br /&gt;
fig6&lt;br /&gt;
&lt;br /&gt;
Energy Usage Comparison&lt;br /&gt;
fig 7&lt;br /&gt;
&lt;br /&gt;
Conclusion(this can be put in the main conclusion) :: It is observed that   Energy Concerned  Factory  Method  Pattern can be obtained from the combination     of     ConcreteProduct     classes     and ConcreteCreator    struct    with    static    FactoryMethod(). The ECFactory  Method  Pattern  consumes  less  CPU  time  than  regular Factory  Method  Pattern  around  11%  while  it  is  better  than  the worst case around 80%.  The bigger the ConcreteProduct type size is, the more range between the best and the worst case in term of energy consumption will be.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29628</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=29628"/>
		<updated>2009-11-19T04:21:57Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Design Pattern for Mobile Applications */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Design Pattern for Mobile Applications ==&lt;br /&gt;
&lt;br /&gt;
The popularity of mobile applications is rising.  Design patterns are useful in almost any coding tasks.  While almost any design pattern could be used within mobile applications, certain patterns are well suited for the unique environment of mobile applications.  This wiki explains the mobile application environment, then focuses on design patterns that work well within this new emerging environment.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
== The Mobile Application Environment ==&lt;br /&gt;
&lt;br /&gt;
The mobile application environment is new and presents many unique opportunities and challenges for developers.  It seems almost everyone now has a cellphone, blackberry, or other small portable device.  These devices are &amp;quot;getting smarter&amp;quot; in that many people are developing new applications for them.  Cell phones no longer are just used to call people, they often have features like text messaging, cameras, video games, web browsers and more.  There is much opportunity for developers to write new applications for mobile devices.&lt;br /&gt;
&lt;br /&gt;
While opportunity abounds, there are also quite a few challenges.  Mobile applications have less resources than are typically found in other development environments.  The screen om mobile devices is typically smaller, there is usually not a keyboard and mouse (user inputs are reduced) [5].  CPU resources are generally less than those found in the typical environment [7].  Power usage is also limited because a battery is typically used, so reducing power use by the application is an important goal [7].  Connections are typically expensive and are often prone to loss of signals [6].&lt;br /&gt;
&lt;br /&gt;
Another problem is that there is a high demand for applications to be developed quickly [7].  The mobile environment is changing rapidly [2].  Some of the lower level programming languages were useful because they produced fast applications, but they took too long to develop with.  Now, with the increased demand for applications quickly, Object Oriented Programming (OOP) is used more.  While this causes a greater usage of resources, this is outweighed by the much quicker development cycle and the re-use of code.  In particular, good solutions may be re-used in OOP [7].&lt;br /&gt;
&lt;br /&gt;
In the mobile environment, applications are often written on different operating systems on different mobile systems.  There is increased interaction between mobile systems, especially for cell phones.  The Service Oriented Architecture design pattern is especially well suited for this situation.  This is a fairly new field, and methods are currently being developed to aid developers.&lt;br /&gt;
&lt;br /&gt;
== Ways to Adapt Using Design Patterns ==&lt;br /&gt;
&lt;br /&gt;
Several principles and patterns have been understood and developed in developing for mobile applications.  Many of these methods are still undergoing change and experimentation.  There are new methods being developed and others left behind.  A design pattern can be defined as a repeatable solution to a common problem in software.  It is a template for how to solve the problem, not actual code [7].&lt;br /&gt;
&lt;br /&gt;
Ease of navigation is a particularly important principle, because of the reduced number of user inputs (for example, buttons from 0 to 9 instead of a mouse and keyboard).  Everything in an application should be no more than three clicks away.  If a program is too &amp;quot;deep&amp;quot;, requiring many clicks, this will be a problem because it takes longer to navigate on a mobile device.  If the application is to &amp;quot;broad&amp;quot; as in too many links on one page, the data may not all fit on the screen.  One way to mitigate this problem is to reduce functionality, to keep the application as simple as possible [5].&lt;br /&gt;
&lt;br /&gt;
!!!! Insert EC Factory here&lt;br /&gt;
&lt;br /&gt;
The [http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html Model View Controller] pattern [1] fits the mobile environment well.  Models may be set up so that optimum data structures are used, as was discussed for the EC Factory pattern [7].  Effecient designs may be built into the controllers.  Views may be standardized for the application to handle the small screens.&lt;br /&gt;
&lt;br /&gt;
The meeting pattern is especially useful in mobile environments.  One device or application serves as the meeting manager.  The meeting manager coordinates and tracks the other agents (programs or applications).  These agents communicate with the oversight of the meeting manager.  Agents may be located on different devices, even on computers with more resources.  This is especially well suited for the mobile environment because it is a good way to manage the communications and relatively small applications.  A further benefit of the meeting pattern is that mobile applications may be combined (somewhat in the fashion of parallel computing) to do more and provide more resources [6].&lt;br /&gt;
&lt;br /&gt;
The master slave pattern is simmilar to the meeting pattern.  Instead of a meeting manager and agents, there is a master that creates, uses, and destroys slave objects.  The master assigns the slaves tasks to complete.  Like agents in the meeting pattern, slaves may be located on different mobile devices, or even on computers that have more resources.  The master / slave pattern is very useful for performing actions in parallel on different mobile devices [6].&lt;br /&gt;
&lt;br /&gt;
== Conclusion ==&lt;br /&gt;
In conclusion, certain design patterns are especially useful in the mobile design environment.  These patterns help developers to produce applications quickly and applications which make efficient use of the limited resources in the mobile environment.  Most design patterns can be of benefit to mobile applications, but certain patterns have features that are especially well suited.  The EC factory pattern was designed for the mobile environment to reduce energy use [7].  A few other patterns show much promise with mobile devices, including the meeting pattern, master / slave pattern, and service oriented architectures.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&lt;br /&gt;
1. http://www.javaworld.com/javaworld/jw-10-2005/jw-1003-mvc.html&lt;br /&gt;
&lt;br /&gt;
2. http://patterns.littlespringsdesign.com/index.php/The_mobile_challenge&lt;br /&gt;
&lt;br /&gt;
3. http://pg-server.csc.ncsu.edu/mediawiki/index.php/CSC/ECE_517_Fall_2009/wiki2_17_va - Service Oriented Architecture&lt;br /&gt;
&lt;br /&gt;
4. http://wiki.forum.nokia.com/index.php/Understading_Desing_Patterns - Understanding Design Patterns&lt;br /&gt;
&lt;br /&gt;
5. http://dev.opera.com/articles/view/designing-and-developing-mobile-web-site/&lt;br /&gt;
&lt;br /&gt;
6. Emerson F.A Lima, Patrícia D. L. Machado, Fláavio R. Sampaio, and Jorge C. A. Figueiredo. ACM SIGSOFT Software Engineering Notes.  Volume 29, issue 3 ISSN: 0163-5948 http://portal.acm.org/citation.cfm?id=986710.986726 Universidade Federal de Campina Grande, Paraíba, Brazil, May, 2004.&lt;br /&gt;
&lt;br /&gt;
7. Kayun Chantarasathaporn, Chonawat Srisa-an. ACM International Conference Proceeding Series; Vol. 270 Proceedings of the 3rd international conference on Mobile technology, applications &amp;amp; systems SESSION: Mobile applications, article 29. ISBN:1-59593-519-3 http://portal.acm.org/citation.cfm?id=1292331.1292364 Bangkok, Thailand, 2006&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:ECFactory_Fig1.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig2.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig5.jpg]]&lt;br /&gt;
[[Image:ECFactory_Fig6.jpg]]&lt;br /&gt;
[[Image:MCFigure3.jpg]]&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=26653</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=26653"/>
		<updated>2009-11-11T20:21:58Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Design Pattern for Mobile Application */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Design Pattern for Mobile Applications ==&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=26482</id>
		<title>CSC/ECE 517 Fall 2009/wiki3 21 MobileDesignPattern</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki3_21_MobileDesignPattern&amp;diff=26482"/>
		<updated>2009-11-09T16:55:08Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
== Design Pattern for Mobile Application ==&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_2_AJAXandMVC&amp;diff=25622</id>
		<title>CSC/ECE 517 Fall 2009/wiki2 2 AJAXandMVC</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki2_2_AJAXandMVC&amp;diff=25622"/>
		<updated>2009-10-11T03:56:13Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* References */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''AJAX AND MVC'''&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
''There are several MVC frameworks that can take advantage of the client-side processing facilities of AJAX. Our aim in writing this wiki page is to consider what parts of the MVC framework (views, controllers?) can migrate part of their functionality to AJAX. In analyzing this problem, we would be considering some server-side languages such as Java, PHP, and Ruby, as well as the .NET framework.''&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== What is MVC ? ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller '''Model–View–Controller (MVC)'''] is an architectural pattern used in software engineering. The pattern isolates business logic from input and presentation, permitting independent development, testing and maintenance of each.MVC is often seen in web applications where the view is the [http://en.wikipedia.org/wiki/Html HTML] or [http://en.wikipedia.org/wiki/Xhtml XHTML] generated by the app. The controller receives [http://en.wikipedia.org/wiki/HTTP#Request_methods GET] or [http://en.wikipedia.org/wiki/HTTP#Request_methods POST] input and decides what to do with it, handing over to domain objects (ie the model) which contain the business rules and know how to carry out specific tasks such as processing a new subscription.MVC helps to reduce the complexity in architectural design and to increase flexibility and reuse of code.&lt;br /&gt;
&lt;br /&gt;
[[Image:ajaxandmvc.gif]]&lt;br /&gt;
&lt;br /&gt;
=== What is AJAX ? ===&lt;br /&gt;
[http://en.wikipedia.org/wiki/AJAX AJAX] stands for Asynchronous JavaScript and [http://en.wikipedia.org/wiki/XML XML].It is a Web development technique for creating web applications. It Makes web pages more responsive by exchanging small amounts of data. It allows the web page to change its content without refreshing the whole page. A web browser technology is independent of web server software. It gives desktop application a feel good factor,by doing behind the scene server communication. A famous example: [http://www.google.com/support/websearch/bin/answer.py?hl=en&amp;amp;answer=106230 Google's suggest] uses Ajax, and it also helped in making it popular.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
Improves the user experience in,&lt;br /&gt;
&lt;br /&gt;
# Analyzing information typed into browser in real time&lt;br /&gt;
# Provide a richer experience&lt;br /&gt;
# Increases responsiveness of web pages&lt;br /&gt;
&lt;br /&gt;
Improve bandwidth utilization&lt;br /&gt;
&lt;br /&gt;
# Only data which is required is retrieved from the server&lt;br /&gt;
&lt;br /&gt;
How does the magic work??&lt;br /&gt;
[[Image:Ajax_fig1.png|left]] [[Image:ajax_fig2.png|none]]&lt;br /&gt;
AJAX runs in your browser. &lt;br /&gt;
Works with asynchronous data transfers(HTTP requests)  between the browser and the web server.&lt;br /&gt;
HTTP requests are sent by [http://en.wikipedia.org/wiki/JavaScript JavaScript] calls without having to  submit a form.&lt;br /&gt;
XML is commonly used as the format for receiving server data but plain text and some other format may be used as well.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Instead of loading a web page, at the start of the session, the browser loads an Ajax engine — written in JavaScript and usually tucked away in a hidden frame. This engine is responsible for both rendering the interface the user sees and communicating with the server on the user’s behalf. &lt;br /&gt;
&amp;lt;br&amp;gt;The Ajax engine allows the user’s interaction with the application to happen asynchronously — independent of communication with the server. So the user is never staring at a blank browser window and an hourglass icon, waiting around for the server to do something. &amp;lt;br&amp;gt;&lt;br /&gt;
Every user action that normally would generate an HTTP request takes the form of a JavaScript call to the Ajax engine instead. Any response to a user action that doesn’t require a trip back to the server — such as simple data validation, editing data in memory, and even some navigation — the engine handles on its own. If the engine needs something from the server in order to respond — if it’s submitting data for processing, loading additional interface code, or retrieving new data — the engine makes those requests asynchronously, usually using XML, without stalling a user’s interaction with the application &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Technologies used in AJAX.&lt;br /&gt;
# JavaScript - JavaScript is a loosely typed object based scripting language supported by all major browsers and essential for   AJAX interactions. JavaScript functions in a page are invoked as event handlers when an event in a page occurs such as a page load, a mouse click, or a key press in a form element.&lt;br /&gt;
# DOM - is API for accessing and manipulating structured documents. In most cases DOM represent the structure of XML or HTML documents.&lt;br /&gt;
# CSS - Allows you to define the presentation of a page such as fonts, colors, sizes, and positioning. CSS allow for a clear separation of the style from the content and may be changed pragmatically by JavaScript.&lt;br /&gt;
# HTTP - Understanding the basic request/response interaction model of HTTP is important for a developer using AJAX. You will be &lt;br /&gt;
exposed to the GET and PUT method when configuring an [http://en.wikipedia.org/wiki/XMLHttpRequest XMLHttpRequest] and HTTP response codes when processing callback.&lt;br /&gt;
&lt;br /&gt;
Some of the drawbacks:[http://en.wikipedia.org/wiki/Ajax_%28programming%29]&lt;br /&gt;
# AJAX interfaces are substantially harder to develop properly than static pages. &amp;lt;br&amp;gt;&lt;br /&gt;
# Pages dynamically created using successive AJAX requests do not automatically register themselves with the browser's history engine, so clicking the browser's &amp;quot;back&amp;quot; button may not return the user to an earlier state of the AJAX-enabled page, but may instead return them to the last full page visited before it. Workarounds include the use of invisible IFrames to trigger changes in the browser's history and changing the anchor portion of the URL (following a #) when AJAX is run and monitoring it for changes.&amp;lt;br&amp;gt;&lt;br /&gt;
# Dynamic web page updates also make it difficult for a user to bookmark a particular state of the application. Solutions to this problem exist, many of which use the URL fragment identifier (the portion of a URL after the '#') to keep track of, and allow users to return to, the application in a given state.&amp;lt;br&amp;gt;&lt;br /&gt;
# Because most web crawlers do not execute JavaScript code, publicly indexable web applications should provide an alternative means of accessing the content that would normally be retrieved with AJAX, to allow search engines to index it. &amp;lt;br&amp;gt;&lt;br /&gt;
# Any user whose browser does not support JavaScript or XMLHttpRequest, or simply has this functionality disabled, will not be able to properly use pages which depend on AJAX. Similarly, devices such as mobile phones, PDAs, and screen readers may not have support for the required technologies. Screen readers that are able to use AJAX may still not be able to properly read the dynamically generated content. The only way to let the user carry out functionality is to fall back to non-JavaScript methods. This can be achieved by making sure links and forms can be resolved properly and do not rely solely on AJAX. In JavaScript, form submission could then be halted with &amp;quot;return false&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
# The same origin policy prevents some AJAX techniques from being used across domains, although the W3C has a draft of the XMLHttpRequest object that would enable this functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
# AJAX opens up another attack vector for malicious code that web developers might not fully test for.&amp;lt;br&amp;gt;&lt;br /&gt;
# AJAX-powered interfaces may dramatically increase the number of user-generated requests to web servers and their back-ends (databases, or other). This can lead to longer response times and/or additional hardware needs. &amp;lt;br&amp;gt;&lt;br /&gt;
# User interfaces can be confusing or behave inconsistently when normal web patterns are not followed. &amp;lt;br&amp;gt;&lt;br /&gt;
# Due to multiple dependent asynchronous requests, you can’t rely on any order of operations in classical AJAX models.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How does AJAX fit with MVC ? ===&lt;br /&gt;
Ajax functionality needs to be implemented first in the view in terms of the call made and then in controller for handling the call . The view should have the call to the  Ajax action which is implemented in controller.&lt;br /&gt;
MVC Controller needs to have a ajax-action  class to take whatever action is required on the business model in model. Action methods can do things like render different views, render a portion of the user interface defined within a partial view as opposed to the full page. It can be consider that any behavioral JavaScript as part of the Controller, including attaching events as well as sending HTTP requests.&lt;br /&gt;
AJAX tag libraries [http://ajaxtags.sourceforge.net/] can be to fit ajax into any framework.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Moreover, there has been AJAX MVC[http://ajax-mvc.sourceforge.net/] used for much smoother integration of ajax into mvc framework.&lt;br /&gt;
&lt;br /&gt;
=== AJAX IN RAILS ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] provide support for AJAX in the form of helper methods. There is no need to add [http://en.wikipedia.org/wiki/JavaScript JavaScript] code in the view templates. Instead, the helper methods create the JavaScript code in the HTML page derived from the view templates. One of the ways this could be understood is through an example [http://onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html] of link_to_remote() method which can be used to get the latest time from a server and display it on a web page.Here is a sample view template.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''' SERVER SIDE CODE '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;html&amp;gt;&lt;br /&gt;
  &amp;lt;head&amp;gt;&lt;br /&gt;
    &amp;lt;title&amp;gt;Ajax Demo&amp;lt;/title&amp;gt;&lt;br /&gt;
    &amp;lt;%= javascript_include_tag &amp;quot;prototype&amp;quot; %&amp;gt;&lt;br /&gt;
  &amp;lt;/head&amp;gt;&lt;br /&gt;
  &amp;lt;body&amp;gt;&lt;br /&gt;
    &amp;lt;h1&amp;gt;What time is it?&amp;lt;/h1&amp;gt;&lt;br /&gt;
    &amp;lt;div id=&amp;quot;time_div&amp;quot;&amp;gt;&lt;br /&gt;
      I don't have the time, but&lt;br /&gt;
      &amp;lt;%= link_to_remote( &amp;quot;click here&amp;quot;,&lt;br /&gt;
                         :update =&amp;gt; &amp;quot;time_div&amp;quot;,&lt;br /&gt;
                         :url =&amp;gt;{ :action =&amp;gt; :say_when }) %&amp;gt;&lt;br /&gt;
      and I will look it up.&lt;br /&gt;
    &amp;lt;/div&amp;gt;&lt;br /&gt;
  &amp;lt;/body&amp;gt;&lt;br /&gt;
&amp;lt;/html&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
What this view does this is that it gives a link to display the current time. The current time would be fetched in the background without refreshing the browser. Now, there are two main helper methods to do this job. One of them is javascript_include_tag() that includes the [http://en.wikipedia.org/wiki/Prototype_JavaScript_Framework Prototype JavaScript library]. This is included with Rails package and is a basic requirement to make use of AJAX functionality.  The other is link_to_remote() call that talks to the remote server when a request for current time is made.&lt;br /&gt;
Here is a brief explanation of the parameters used in this method:&lt;br /&gt;
&lt;br /&gt;
#'''click_here''' :  this is the text for displaying the link&lt;br /&gt;
#'''time_div'''  : it is the id of the HTML DOM element  that would have the current time content&lt;br /&gt;
#'''url'''       : refers to the server side action and it is say_when in this case&lt;br /&gt;
&lt;br /&gt;
[[Image:ajaxinrails_fig1.png]]                        [[Image:ajaxinrails_fig2.png]]&lt;br /&gt;
&lt;br /&gt;
The role of the index action is to render the index.html file which displays the ‘before’ view. The controller is named demo and is coded as,&lt;br /&gt;
&lt;br /&gt;
''' SERVER SIDE CODE '''&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class DemoController &amp;lt; ApplicationController&lt;br /&gt;
  def index&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def say_when&lt;br /&gt;
    render_text &amp;quot;&amp;lt;p&amp;gt;The time is &amp;lt;b&amp;gt;&amp;quot; + DateTime.now.to_s + &amp;quot;&amp;lt;/b&amp;gt;&amp;lt;/p&amp;gt;&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&lt;br /&gt;
When  “click here” is clicked on we would see the ‘after’ view which is shown above. What happens over here is that when a user clicks on “click me” an [http://en.wikipedia.org/wiki/XMLHttpRequest XMLHttpRequest] is created by the browser and sent to the server where  in the server invokes the say_when action and renders the HTML response fragment containing the current time. When the client side JavaScript receives the response it replaces the contents of the div with an id of time_div.Besides the above example, there are other AJAX helper methods like the form method, i.e., form_remote_tag(). Here, is a prototype for it.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;%= form_remote_tag :url =&amp;gt; { :action =&amp;gt; 'create' },&lt;br /&gt;
  		     :update =&amp;gt; ‘ajax_result’ %&amp;gt; 	&lt;br /&gt;
	&amp;lt;%= render :partial =&amp;gt; ‘form’ %&amp;gt; &lt;br /&gt;
	&amp;lt;%= submit_tag &amp;quot;Create&amp;quot; %&amp;gt;&lt;br /&gt;
&amp;lt;%= end_form_tag %&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div id=&amp;quot;ajax_result&amp;quot;&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Ajax in Ruby has immense potential in its application and can add to the efficiency as well as to the looks of a web template. In order to get a more in depth foot in Ajax on Rails , there is a very popular book [http://oreilly.com/catalog/9780596527440 “Ajax on Rails”] by Scott Raymond and published by O’Reilly.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== AJAX IN PHP ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/PHP PHP] supports [http://en.wikipedia.org/wiki/JSON JSON] encoding by  default, this allows you to pass complex data types back and forth  between PHP and Javascript fairly easily.Consider this simple example.[http://www.ajaxf1.com/tutorial/ajax-php.html?page=2]&lt;br /&gt;
	&lt;br /&gt;
To demonstrate the AJAX PHP connection we will create a very simple form with 2 input fields. In the first field you can type any text and we will send this text to our PHP script which will convert it to uppercase and sends it back to us.&amp;lt;br&amp;gt;&lt;br /&gt;
[[Image:Ajax-mvc-php.PNG ]]&lt;br /&gt;
&lt;br /&gt;
''' CLIENT SIDE CODE ''' &lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;script&amp;gt;&lt;br /&gt;
// Get the HTTP Object&lt;br /&gt;
function getHTTPObject(){&lt;br /&gt;
   if (window.ActiveXObject) &lt;br /&gt;
       return new ActiveXObject(&amp;quot;Microsoft.XMLHTTP&amp;quot;);&lt;br /&gt;
   else if (window.XMLHttpRequest)//catches the response from the server &lt;br /&gt;
       return new XMLHttpRequest();//this object is for AJAX PHP communication &lt;br /&gt;
   else {&lt;br /&gt;
      alert(&amp;quot;Your browser does not support AJAX.&amp;quot;);&lt;br /&gt;
      return null;&lt;br /&gt;
   }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Change the value of the outputText field&lt;br /&gt;
function setOutput(){&lt;br /&gt;
    if(httpObject.readyState == 4){&lt;br /&gt;
        document.getElementById('outputText').value = httpObject.responseText;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Implement business logic    &lt;br /&gt;
function doWork(){    &lt;br /&gt;
    httpObject = getHTTPObject();&lt;br /&gt;
    if (httpObject != null) {&lt;br /&gt;
        httpObject.open(&amp;quot;GET&amp;quot;, &amp;quot;upperCase.php?inputText=&amp;quot;&lt;br /&gt;
                        +document.getElementById('inputText').value, true);&lt;br /&gt;
        httpObject.send(null); &lt;br /&gt;
        httpObject.onreadystatechange = setOutput;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/script&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;form name=&amp;quot;testForm&amp;quot;&amp;gt;&lt;br /&gt;
     Input text: &amp;lt;input type=&amp;quot;text&amp;quot;  onkeyup=&amp;quot;doWork();&amp;quot; name=&amp;quot;inputText&amp;quot; id=&amp;quot;inputText&amp;quot; /&amp;gt; &lt;br /&gt;
     Output text: &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;outputText&amp;quot; id=&amp;quot;outputText&amp;quot; /&amp;gt;&lt;br /&gt;
  &amp;lt;/form&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''' SERVER SIDE CODE '''&lt;br /&gt;
&lt;br /&gt;
Server side functionality is very simple compared to the client side. In the PHP code we just need to check the $_GET super-global array. Afterwards convert it to uppercase and echo the result.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;lt;?php&lt;br /&gt;
    if (isset($_GET['inputText'])) &lt;br /&gt;
       echo strtoupper($_GET['inputText']);&lt;br /&gt;
?&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== AJAX IN .NET Framework ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/ASP.NET ASP.NET] AJAX is the free Microsoft AJAX framework for building highly interactive and responsive web applications that work across all popular browsers. The ASP.NET AJAX framework includes Server-Side ASP.NET AJAX, Client-Side ASP.NET AJAX, the AJAX Control Toolkit, and the [http://en.wikipedia.org/wiki/JQuery jQuery] library. ASP.NET AJAX enables developers to choose their preferred method of AJAX development, whether it is server-side programming, client-side programming, or a combination of both.ASP.NET AJAX server-side controls such as the ScriptManager, UpdatePanel, and the UpdateProgress control to add AJAX functionality to an ASP.NET application without writing any JavaScript. For example, the UpdatePanel control enables you to update a portion of an ASP.NET page without requiring you to reload the entire page. The ScriptManager control enables you to manage browser history in an AJAX application by updating the browser back button after an AJAX request. &lt;br /&gt;
&lt;br /&gt;
If you prefer to work directly with JavaScript then you can take advantage of client-side ASP.NET AJAX. The client-side ASP.NET AJAX Library provides a foundation for building rich client-side applications. The library simplifies cross-browser development of client-side applications. For example, the library enables you to call web services and create components and controls -- all through pure client-side JavaScript code. &lt;br /&gt;
&lt;br /&gt;
One cool thing:	AJAX Extensions for .NET&lt;br /&gt;
No need to modify server-side code&lt;br /&gt;
No javascript functions need to be  added&lt;br /&gt;
AJAX can be enabled/disabled by  changing one line of code (“Script  Manager”)&lt;br /&gt;
 Its as simple as 1 2 3 :)&lt;br /&gt;
# “Drag and Drop” Script Manager  Control onto your form&lt;br /&gt;
# Place “Update Panels” around  content you wish to have async  updates for&lt;br /&gt;
# Add triggers to the update panels  to tell them which events to update  on.&lt;br /&gt;
&lt;br /&gt;
=== AJAX IN JAVA ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Java Java] technology and AJAX work well together. Java technology provides the server-side processing for AJAX interactions. It can provide this through servlets, [http://en.wikipedia.org/wiki/JavaServer_Pages JavaServer Pages (JSP)]  technology, [http://en.wikipedia.org/wiki/JavaServer_Faces JavaServer Faces (JSF)] technology, and web services. The programming model for handling AJAX requests uses the same APIs that you would use for conventional web applications. JSF technology can be used to create reusable components that generate the client-side JavaScript and corresponding server-side AJAX processing code. &lt;br /&gt;
&lt;br /&gt;
Consider a simple example [http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html] where AJAX and java servlets interact.HTML page generated in JSP technology contains an HTML form that requires server-side logic to validate form data without refreshing the page. A server-side web component (servlet) named ValidateServlet will provide the validation logic. &lt;br /&gt;
&lt;br /&gt;
[[Image:railsinjava_fig1.png]]&lt;br /&gt;
&lt;br /&gt;
Ajax interaction as they appear in Figure:&lt;br /&gt;
# A client event occurs.[http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#event_occurs] &lt;br /&gt;
# An XMLHttpRequest object is created and configured. [http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#configure_xmlhttprequest] &lt;br /&gt;
# The XMLHttpRequest object makes a call.[http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#make_call] &lt;br /&gt;
# The request is processed by the ValidateServlet.[http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#serverside_processing] &lt;br /&gt;
# The ValidateServlet returns an XML document containing the result.[http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#xml_returned] &lt;br /&gt;
# The XMLHttpRequest object calls the callback() function and processes the result.[http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#post_process] &lt;br /&gt;
# The HTML DOM is updated.[http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html#update_dom]&lt;br /&gt;
&lt;br /&gt;
=== Conclusion ===&lt;br /&gt;
&lt;br /&gt;
[http://en.wikipedia.org/wiki/Rich_Internet_application Rich Internet applications (RIA)] are web application that approximate the look and feel and usability of desktop application. It adds performance and rich GUI. AJAX enables RIA, which uses client-side scripting to make web applications more responsive. Ajax application separates client-side user interaction and server communication, and run them in parallel, reducing the delays of server-side processing normally experienced by user.&lt;br /&gt;
&lt;br /&gt;
MVC frameworks in various languages like PHP, Rails, .NET etc provides well defined support to implement AJAX functionality. This article provided a brief overview of this support in Java, .NET, Rails and PHP.&lt;br /&gt;
&lt;br /&gt;
=== References ===&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller&lt;br /&gt;
&lt;br /&gt;
* http://en.wikipedia.org/wiki/Ajax_%28programming%29&lt;br /&gt;
&lt;br /&gt;
* http://www.ajaxf1.com/tutorial/ajax-php.html?page=2&lt;br /&gt;
&lt;br /&gt;
* http://www.asp.net/ajax/&lt;br /&gt;
&lt;br /&gt;
* http://java.sun.com/developer/technicalArticles/J2EE/AJAX/index.html&lt;br /&gt;
&lt;br /&gt;
* http://www.phpied.com/ajax-mvc/&lt;br /&gt;
&lt;br /&gt;
* http://www.php.net/&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18765</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18765"/>
		<updated>2009-09-08T19:05:21Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* For Overview */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18764</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18764"/>
		<updated>2009-09-08T19:05:12Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* For Ideas on test case design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18763</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18763"/>
		<updated>2009-09-08T19:05:02Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Step 1:  Installing JUnit */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18762</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18762"/>
		<updated>2009-09-08T19:04:42Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Step 2: Writing a Test Case */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18761</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18761"/>
		<updated>2009-09-08T19:04:22Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Step 3: Writing a Test Suite */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18760</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18760"/>
		<updated>2009-09-08T19:04:10Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Step 4: Running the Test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18759</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18759"/>
		<updated>2009-09-08T19:03:54Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Step 4: Running the Test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18758</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18758"/>
		<updated>2009-09-08T19:03:37Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Step 4: Running the Test */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18757</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18757"/>
		<updated>2009-09-08T19:03:28Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Rules for designing effective JUnit Test cases */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18756</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18756"/>
		<updated>2009-09-08T19:03:11Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Using Test Fixture for a common set of objects */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18755</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18755"/>
		<updated>2009-09-08T19:02:57Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* To Test a method that doesnot return anything */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18754</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18754"/>
		<updated>2009-09-08T19:02:47Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* To test an expected/unexpected exception */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18753</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18753"/>
		<updated>2009-09-08T19:02:32Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* To Test protected/private methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18752</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18752"/>
		<updated>2009-09-08T19:02:15Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Conditions to test get() and set() methods */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18751</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18751"/>
		<updated>2009-09-08T19:01:57Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* To run setUp() and tearDown() code once for all tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment. &lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18750</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18750"/>
		<updated>2009-09-08T19:01:42Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment. &lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18749</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18749"/>
		<updated>2009-09-08T19:00:51Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* Conclusion */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment. &lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
By following the guidelines given in this articles, one can write a good unit test using junit, which is as important as writing good quality code.&amp;lt;br&amp;gt;&lt;br /&gt;
A good quality unit test will take care of all classes, boundary conditions, domain data and functionality.&amp;lt;br&amp;gt;&lt;br /&gt;
A good test can be creative to explore all possibilities, thinking out of box.&amp;lt;br&amp;gt;&lt;br /&gt;
Finally automating the test using tool like junit which is in the native java language itself helps in saving manual intervention and time/money.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18739</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18739"/>
		<updated>2009-09-08T18:30:30Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class BooksCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private BooksCart cart1;&lt;br /&gt;
    private BooksCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new BooksCart(6,8);&lt;br /&gt;
        cart2 = new BooksCart(7,10);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new BooksCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
- The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
* static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&lt;br /&gt;
- Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
'''To write a test suite, follow these steps:'''&amp;lt;br&amp;gt; &lt;br /&gt;
* Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class BooksCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The BooksCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(BooksCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The BooksCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner BooksCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for coding effective JUnit Tests==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
    private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void setUp() {&lt;br /&gt;
    collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testEmptyCollection() {&lt;br /&gt;
    assertTrue(collection.isEmpty());&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testOneItemCollection() {&lt;br /&gt;
    collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
    assertEquals(1, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testEmptyCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
setUp()&amp;lt;br&amp;gt;&lt;br /&gt;
testOneItemCollection()&amp;lt;br&amp;gt;&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
    private File output;&lt;br /&gt;
&lt;br /&gt;
    @Before&lt;br /&gt;
    public void createOutputFile() {&lt;br /&gt;
    output = new File(...);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @After&lt;br /&gt;
    public void deleteOutputFile() {&lt;br /&gt;
    output.delete();&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Test&lt;br /&gt;
    public void testSomethingWithFile() {&lt;br /&gt;
    ...&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
    ArrayList emptyList = new ArrayList();&lt;br /&gt;
    Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment. &lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
* http://articles.techrepublic.com.com/5100-10878_11-1027676.html&lt;br /&gt;
* http://msdn.microsoft.com/en-us/library/aa292197%28VS.71%29.asp&lt;br /&gt;
* http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
* http://clarkware.com/articles/JUnitPrimer.html#usage&lt;br /&gt;
* http://users.csc.calpoly.edu/~jdalbey/205/Resources/junitQuickStart.html&lt;br /&gt;
* http://www.cis.upenn.edu/~matuszek/cit591-2004/Lectures/&lt;br /&gt;
* http://en.wikipedia.org/wiki/JUnit&lt;br /&gt;
* http://www.laliluna.de/eclipse-junit-testing-tutorial.html&lt;br /&gt;
* http://junit.sourceforge.net/doc/faq/faq.htm&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18533</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18533"/>
		<updated>2009-09-08T02:14:22Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;br&amp;gt;&lt;br /&gt;
'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
''JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.''&lt;br /&gt;
&lt;br /&gt;
The objective of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== JUnit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;export CLASSPATH=$JUNIT_HOME/junit.jar&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class ShoppingCartTest extends TestCase&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class ShoppingCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private ShoppingCart cart1;&lt;br /&gt;
    private ShoppingCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new ShoppingCart(6,8);&lt;br /&gt;
        cart2 = new ShoppingCart(3,9);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new ShoppingCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
2. static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test) &amp;lt;br&amp;gt;&lt;br /&gt;
* Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
* The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
3. static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&amp;lt;br&amp;gt; &lt;br /&gt;
* Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
To write a test suite, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
1. Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
2. Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class ShoppingCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The ShoppingCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(ShoppingCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner ShoppingCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner ShoppingCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The ShoppingCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner ShoppingCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective JUnit Test cases ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for writing effective Junit Test cases==&lt;br /&gt;
&lt;br /&gt;
=== Using Test Fixture for a common set of objects ===&lt;br /&gt;
Using a test fixture avoids duplicating the code necessary to initialize (and cleanup) the common objects.&lt;br /&gt;
Tests can use the objects (variables) in a test fixture, with each test invoking different methods on objects in the fixture and asserting different expected results. Each test runs in its own test fixture to isolate tests from the changes made by other tests. That is, tests don't share the state of objects in the test fixture. Because the tests are isolated, they can be run in any order.&lt;br /&gt;
To create a test fixture, declare instance variables for the common objects. Initialize these objects in a public void method annotated with @Before. The JUnit framework automatically invokes any @Before methods before each test is run.&lt;br /&gt;
The following example shows a test fixture with a common Collection object.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.util.*;&lt;br /&gt;
&lt;br /&gt;
public class SimpleTest {&lt;br /&gt;
&lt;br /&gt;
private Collection&amp;lt;Object&amp;gt; collection;&lt;br /&gt;
&lt;br /&gt;
@Before&lt;br /&gt;
public void setUp() {&lt;br /&gt;
collection = new ArrayList&amp;lt;Object&amp;gt;();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Test&lt;br /&gt;
public void testEmptyCollection() {&lt;br /&gt;
assertTrue(collection.isEmpty());&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
@Test&lt;br /&gt;
public void testOneItemCollection() {&lt;br /&gt;
collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
assertEquals(1, collection.size());&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Given this test, the methods might execute in the following order:&lt;br /&gt;
setUp()&lt;br /&gt;
testEmptyCollection()&lt;br /&gt;
setUp()&lt;br /&gt;
testOneItemCollection()&lt;br /&gt;
The ordering of test-method invocations is not guaranteed, so testOneItemCollection() might be executed before testEmptyCollection(). But it doesn't matter, because each method gets its own instance of the collection.&lt;br /&gt;
Although JUnit provides a new instance of the fixture objects for each test method, if you allocate any external resources in a @Before method, you should release them after the test runs by annotating a method with @After. The JUnit framework automatically invokes any @After methods after each test is run. For example:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
package junitfaq;&lt;br /&gt;
&lt;br /&gt;
import org.junit.*;&lt;br /&gt;
import static org.junit.Assert.*;&lt;br /&gt;
import java.io.*;&lt;br /&gt;
&lt;br /&gt;
public class OutputTest {&lt;br /&gt;
&lt;br /&gt;
private File output;&lt;br /&gt;
&lt;br /&gt;
@Before&lt;br /&gt;
public void createOutputFile() {&lt;br /&gt;
output = new File(...);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@After&lt;br /&gt;
public void deleteOutputFile() {&lt;br /&gt;
output.delete();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
@Test&lt;br /&gt;
public void testSomethingWithFile() {&lt;br /&gt;
...&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
With this test, the methods will execute in the following order:&lt;br /&gt;
&lt;br /&gt;
* createOutputFile()&lt;br /&gt;
* testSomethingWithFile()&lt;br /&gt;
* deleteOutputFile()&lt;br /&gt;
&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To test an expected/unexpected exception ===&lt;br /&gt;
* Case of expected exception&lt;br /&gt;
Add the optional expected attribute to the @Test annotation. The following is an example test that passes when the expected IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test(expected=IndexOutOfBoundsException.class)&lt;br /&gt;
public void testIndexOutOfBoundsException() {&lt;br /&gt;
ArrayList emptyList = new ArrayList();&lt;br /&gt;
Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* Case of unexpected exception&lt;br /&gt;
Declare the exception in the throws clause of the test method and don't catch the exception within the test method. Uncaught exceptions will cause the test to fail with an error.&lt;br /&gt;
The following is an example test that fails when the IndexOutOfBoundsException is raised:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
@Test&lt;br /&gt;
public void testIndexOutOfBoundsExceptionNotRaised() &lt;br /&gt;
throws IndexOutOfBoundsException {&lt;br /&gt;
&lt;br /&gt;
ArrayList emptyList = new ArrayList();&lt;br /&gt;
Object o = emptyList.get(0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== To Test protected/private methods ===&lt;br /&gt;
* Case for protected methods&lt;br /&gt;
Place your tests in the same package as the classes under test.&lt;br /&gt;
* Case for private methods&lt;br /&gt;
Testing private methods may be an indication that those methods should be moved into another class to promote reusability.&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment. &lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18470</id>
		<title>CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18470"/>
		<updated>2009-09-08T01:18:23Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;'''Writing Effective JUnit Test Cases'''&amp;lt;br&amp;gt;&lt;br /&gt;
----&lt;br /&gt;
&lt;br /&gt;
JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.&lt;br /&gt;
&lt;br /&gt;
The main aim of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
==Introduction== &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Unit Testing Overview=== &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
''Test Case''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Junit Overview===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
1. An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
2. Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
1. They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
2. They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
1. Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
2. When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
''So what exactly is JUnit ?? ''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
1. Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
2. Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
3. Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
JUnit mailing lists and forums&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit official documentation&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Websites to lookout==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
===For Overview===&lt;br /&gt;
Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
===For Ideas on test case design===&lt;br /&gt;
For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&lt;br /&gt;
==How to get started==&lt;br /&gt;
===Setup===&lt;br /&gt;
===Simple testing example===&lt;br /&gt;
&lt;br /&gt;
==Rules for writing effective Junit test cases==&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References==&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18454</id>
		<title>Talk:CSC/ECE 517 Fall 2009/wiki1a 1 JunitTest</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Talk:CSC/ECE_517_Fall_2009/wiki1a_1_JunitTest&amp;diff=18454"/>
		<updated>2009-09-08T01:08:05Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: /* To run setUp() and tearDown() code once for all tests */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;==Introduction== &lt;br /&gt;
&lt;br /&gt;
=== Writing Effective JUnit Test Cases ===&lt;br /&gt;
&lt;br /&gt;
JUnit is a testing framework aimed at making unit testing easy by simplifying the writing of test cases and by providing automated running of tests and test suites.&lt;br /&gt;
&lt;br /&gt;
The main aim of this document is to provide a repository of rules that can be used in-order to write an effective Junit test.&lt;br /&gt;
This doc also discusses various sites and suggest which are the best sites to read for an overview of the topic and to understand the different philosophies of test-case design.&lt;br /&gt;
&lt;br /&gt;
=== Unit Testing Overview === &lt;br /&gt;
&lt;br /&gt;
The primary goal of unit testing is to take the smallest piece of testable software in the application, isolate it from the remainder of the code, and determine whether it behaves exactly as you expect. &lt;br /&gt;
&lt;br /&gt;
'''Test Case'''&amp;lt;br&amp;gt;&lt;br /&gt;
It describes a scenario that can be exercised through the application.&amp;lt;br&amp;gt;&lt;br /&gt;
Each test case is divided into two parts: input and expected output. &amp;lt;br&amp;gt;&lt;br /&gt;
The input part lists all the test case statements that create variables or assign values to variables.&amp;lt;br&amp;gt;&lt;br /&gt;
The expected output part indicates the expected results; it shows either assertions or the message 'no exception' (when no assertions exist). &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The main issue in constructing a unit test is deciding the scope of test cases.&amp;lt;br&amp;gt;&lt;br /&gt;
If the scope is too narrow, then the tests will be trivial and the objects might pass the tests, but there will be no design of their interactions and interactions of objects are important for object oriented design.&amp;lt;br&amp;gt;&lt;br /&gt;
On the other hand if the scope is too broad, then there is a high chance that not every component of the new code will get tested.&amp;lt;br&amp;gt;&lt;br /&gt;
The programmer is then reduced to testing-by-poking-around, which is not an effective test strategy.&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Junit Overview ===&lt;br /&gt;
Non-automated testing is usually like this:&amp;lt;br&amp;gt;&lt;br /&gt;
* An expression is used in a debugger, you can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects.&amp;lt;br&amp;gt;&lt;br /&gt;
* Expressions are tested as statements which print to the standard output stream.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
But its not preferred because:&amp;lt;br&amp;gt;&lt;br /&gt;
* They require human judgment to analyze their results.&amp;lt;br&amp;gt;&lt;br /&gt;
* They are cumbersome, non repeatable - you can only execute one debug expression at a time and a program with too many print statements causes the dreaded &amp;quot;Scroll Blindness&amp;quot;.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
On the upper hand, JUnit tests, which uses the automated framework does not require human judgment to interpret, and it is easy to run many of them at the same time.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
When you need to test something, here is what you do:&amp;lt;br&amp;gt;&lt;br /&gt;
* Annotate a method with @org.junit.Test&amp;lt;br&amp;gt;&lt;br /&gt;
* When you want to check a value, import org.junit.Assert.* statically, call assertTrue() and pass a boolean that is true if the test succeeds&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''So what exactly is JUnit ?? '''&amp;lt;br&amp;gt;&lt;br /&gt;
It is a open-source unit testing framework for Java and provides a way to organize test data and perform repeatable  test execution.&amp;lt;br&amp;gt;&lt;br /&gt;
In JUnit, one has to write Java code, called a test class, that describes test data, invokes the methods to be tested, and determines test results.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit was originally written by Erich Gamma and Kent Beck.&amp;lt;br&amp;gt;&lt;br /&gt;
The official JUnit home page is http://junit.org.&amp;lt;br&amp;gt;&lt;br /&gt;
JUnit is Open Source Software, released under IBM's Common Public License Version 0.5 and hosted on SourceForge.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
It is an instance of the xUnit architecture for unit testing frameworks. JUnit features include:&amp;lt;br&amp;gt;&lt;br /&gt;
* Assertions for testing expected results&amp;lt;br&amp;gt;&lt;br /&gt;
* Test fixtures for sharing common test data&amp;lt;br&amp;gt;&lt;br /&gt;
* Test runners for running tests&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit mailing lists and forums'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://groups.yahoo.com/group/junit/ JUnit user list]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-announce JUnit announcements]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://lists.sourceforge.net/lists/listinfo/junit-devel JUnit developer list]&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
'''JUnit official documentation'''&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/testinfected/testing.htm JUnit Test Infected: Programmers Love Writing Tests]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookbook/cookbook.htm JUnit Cookbook]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/cookstour/cookstour.htm JUnit - A Cook's Tour]&amp;lt;br&amp;gt;&lt;br /&gt;
[http://junit.sourceforge.net/doc/faq/faq.htm JUnit FAQ]&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Although JUnit testing costs some time and money, but unit testing provides some crucial advantages, viz:&amp;lt;br&amp;gt;&lt;br /&gt;
Automation of the testing process&amp;lt;br&amp;gt;&lt;br /&gt;
Reduces difficulties of discovering errors contained in more complex pieces of the application&amp;lt;br&amp;gt;&lt;br /&gt;
Test coverage is often enhanced because attention is given to each unit.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
For example, &amp;lt;br&amp;gt;&lt;br /&gt;
if you have two units and decide it would be more cost effective to glue them together and initially test them as an integrated unit, an error could occur in a variety of places, now:&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 1?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in unit 2?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to defects in both units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the interface between the units?&amp;lt;br&amp;gt;&lt;br /&gt;
* Is the error due to a defect in the test?&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
Finding the error (or errors) in the integrated module is much more complicated than first isolating the units, testing each, then integrating them and testing the whole.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Websites to lookout ==&lt;br /&gt;
These are some of the best webpages which will help you in getting more description as well as details about Junit. &lt;br /&gt;
&amp;lt;br&amp;gt;Also it will help you understand the test case design philosophy.&amp;lt;br&amp;gt;&lt;br /&gt;
=== For Overview ===&lt;br /&gt;
* Junit testing basics:&amp;lt;br&amp;gt;&lt;br /&gt;
http://articles.techrepublic.com.com/5100-10878_11-1027676.html        &lt;br /&gt;
&lt;br /&gt;
* Basics of Junit:&amp;lt;br&amp;gt; &lt;br /&gt;
http://junit.sourceforge.net/doc/cookbook/cookbook.htm&lt;br /&gt;
http://junit.sourceforge.net/doc/faq/faq.htm#overview&lt;br /&gt;
&lt;br /&gt;
=== For Ideas on test case design ===&lt;br /&gt;
* For successful test automation:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.io.com/~wazmo/papers/seven_steps.html&lt;br /&gt;
&lt;br /&gt;
* Unit testing principles:&amp;lt;br&amp;gt; &lt;br /&gt;
http://www.acm.org/ubiquity/views/t_burns_1.html&lt;br /&gt;
&lt;br /&gt;
* For test suite effectiveness:&amp;lt;br&amp;gt; &lt;br /&gt;
http://java.sys-con.com/node/299945&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== How to get started ==&lt;br /&gt;
=== Step 1:  Installing JUnit ===&lt;br /&gt;
&lt;br /&gt;
* Download latest version of JUnit from &amp;quot;http://sourceforge.net/projects/junit/files/&amp;quot;.&amp;lt;br&amp;gt;&lt;br /&gt;
* Then install JUnit on your platform of choice:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;b&amp;gt;Windows&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Windows, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as %JUNIT_HOME%. &amp;lt;br&amp;gt;&lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
set CLASSPATH=%JUNIT_HOME%\junit.jar&amp;lt;br&amp;gt; &lt;br /&gt;
'''Unix (bash)'''&amp;lt;br&amp;gt; &lt;br /&gt;
To install JUnit on Unix, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
* Unzip the junit.zip distribution file to a directory referred to as $JUNIT_HOME.&amp;lt;br&amp;gt; &lt;br /&gt;
* Add JUnit to the classpath:&amp;lt;br&amp;gt; &lt;br /&gt;
export CLASSPATH=$JUNIT_HOME/junit.jar&lt;br /&gt;
&lt;br /&gt;
=== Step 2: Writing a Test Case === &lt;br /&gt;
To write a test case, follow these steps:&amp;lt;br&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
* Implement a class derived from TestCase, a base class provided by JUnit.&amp;lt;br&amp;gt;&lt;br /&gt;
    public class ShoppingCartTest extends TestCase&amp;lt;br&amp;gt;&lt;br /&gt;
* Be sure the test class belongs to the same package as that of the unit you wish to test&amp;lt;br&amp;gt;&lt;br /&gt;
* Override the setUp() method to initialize object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Optionally override the tearDown() method to release object(s) under test. &amp;lt;br&amp;gt;&lt;br /&gt;
* Define one or more public testXXX() methods that exercise the object(s) under test and assert expected results.&amp;lt;br&amp;gt;&lt;br /&gt;
The following is an example test case:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.TestCase;&lt;br /&gt;
&lt;br /&gt;
public class ShoppingCartTest extends TestCase {&lt;br /&gt;
&lt;br /&gt;
    private ShoppingCart cart1;&lt;br /&gt;
    private ShoppingCart cart2;&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Sets up the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called before every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void setUp() {&lt;br /&gt;
&lt;br /&gt;
        cart1 = new ShoppingCart(6,8);&lt;br /&gt;
        cart2 = new ShoppingCart(3,9);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tears down the test fixture.&lt;br /&gt;
     *&lt;br /&gt;
     * Called after every test case method.&lt;br /&gt;
     */&lt;br /&gt;
    protected void tearDown() {&lt;br /&gt;
        // release objects under test here, if necessary&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Tests emptying the cart.&lt;br /&gt;
     */&lt;br /&gt;
    public void testEmpty() {&lt;br /&gt;
&lt;br /&gt;
        cart1.empty();&lt;br /&gt;
        cart2.empty();&lt;br /&gt;
        assertEquals(cart1, new ShoppingCart(3, 9));&lt;br /&gt;
        assertTrue(!cart2.equals(cart1));&lt;br /&gt;
    }&lt;br /&gt;
   }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;b&amp;gt;Methods used in JUnit:&amp;lt;/b&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1. An assert method is a JUnit method that performs a test, and throws an AssertionFailedError if the test fails &amp;lt;br&amp;gt;&lt;br /&gt;
2. static void assertTrue(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertTrue(String message, boolean test) &amp;lt;br&amp;gt;&lt;br /&gt;
* Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
* The optional message is included in the Error&amp;lt;br&amp;gt;&lt;br /&gt;
3. static void assertFalse(boolean test)&amp;lt;br&amp;gt;&lt;br /&gt;
   static void assertFalse(String message, boolean test)&amp;lt;br&amp;gt; &lt;br /&gt;
* Throws an AssertionFailedError if the test fails&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 3: Writing a Test Suite ===&lt;br /&gt;
Next, we'll write a test suite that includes several test cases. The test suite will allow us to run all of its test cases in one fell swoop.&amp;lt;br&amp;gt; &lt;br /&gt;
To write a test suite, follow these steps:&amp;lt;br&amp;gt; &lt;br /&gt;
1. Write a Java class that defines a static suite() factory method that creates a TestSuite containing all the tests. &amp;lt;br&amp;gt;&lt;br /&gt;
2. Optionally define a main() method that runs the TestSuite in batch mode. &amp;lt;br&amp;gt; &lt;br /&gt;
&lt;br /&gt;
The following is an example test suite:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import junit.framework.Test;&lt;br /&gt;
import junit.framework.TestSuite;&lt;br /&gt;
&lt;br /&gt;
public class ShoppingCartTestSuite {&lt;br /&gt;
  &lt;br /&gt;
    public static Test suite() {&lt;br /&gt;
&lt;br /&gt;
        TestSuite suite = new TestSuite();&lt;br /&gt;
  &lt;br /&gt;
        //&lt;br /&gt;
        // The ShoppingCartTest we created above.&lt;br /&gt;
        //&lt;br /&gt;
        suite.addTestSuite(ShoppingCartTest.class);&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Another example test suite of tests.&lt;br /&gt;
        // &lt;br /&gt;
        suite.addTest(CreditCardTestSuite.suite());&lt;br /&gt;
&lt;br /&gt;
        //&lt;br /&gt;
        // Add more tests here&lt;br /&gt;
        //&lt;br /&gt;
&lt;br /&gt;
        return suite;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    /**&lt;br /&gt;
     * Runs the test suite using the textual runner.&lt;br /&gt;
     */&lt;br /&gt;
    public static void main(String[] args) {&lt;br /&gt;
        junit.textui.TestRunner.run(suite());&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Step 4: Running the Test ===&lt;br /&gt;
Now that we've written a test suite containing a collection of test cases and other test suites, we can run either the test suite or any of its test cases individually. Running a TestSuite will automatically run all of its subordinate TestCase instances and TestSuite instances. Running a TestCase will automatically invoke all of its public testXXX() methods. &lt;br /&gt;
JUnit provides both a textual and a graphical user interface. Both user interfaces indicate how many tests were run, any errors or failures, and a simple completion status. The simplicity of the user interfaces is the key to running tests quickly. You should be able to run your tests and know the test status with a glance, much like you do with a compiler. &lt;br /&gt;
&lt;br /&gt;
To run our test case using the textual user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.textui.TestRunner ShoppingCartTest &amp;lt;/pre&amp;gt;&lt;br /&gt;
The textual user interface displays &amp;quot;OK&amp;quot; if all the tests passed and failure messages if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
To run the test case using the graphical user interface, use:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner ShoppingCartTest &amp;lt;/pre&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The graphical user interface displays a Swing window with a green progress bar if all the tests passed or a red progress bar if any of the tests failed.&amp;lt;br&amp;gt; &lt;br /&gt;
The ShoppingCartTest Suite can be run similarly:&amp;lt;br&amp;gt; &lt;br /&gt;
&amp;lt;pre&amp;gt;java junit.swingui.TestRunner ShoppingCartTestSuite&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Rules for designing effective test cases that is easy to comprehend ==&lt;br /&gt;
* Identify  and assemble a list of all the actions that your program should be able to perform. use cases,nonfunctional requirements specifications, test case specifications, user interface design documents, mockups, user profiles, and various additional artifacts.&lt;br /&gt;
* Identify the code's entry points - central pieces of code that exercise the functionality that the code as a whole is designed to undertake.&lt;br /&gt;
* Pair entry points with the use cases that they implement.&lt;br /&gt;
* Create test cases by applying the initialize/work/check procedure.&lt;br /&gt;
** Initialize: Create the environment that the test expects to run in. The initialization code can either be in the beginning of the test or in the setUp() method.&lt;br /&gt;
** Work: Call the code that is being tested, capturing any interesting output and recording any interesting statistics.&lt;br /&gt;
** Check: Use assert statements to ensure that the code worked as expected.&lt;br /&gt;
* Develop runtime event diagrams and use them to facilitate testing.&lt;br /&gt;
* Follow KISS rule:It's a bad idea to keep the input source name and expression in the test case. Some items (server names, usernames and passwords, etc.) should not live in the test files, which should be configurable for the specific deployment. Rather, design the test cases to facilitate separation of test drivers and test data, test driver reuse over a larger set of data, and test data reuse over a larger set of test drivers. On the other hand, don't over-architect a simple test case implementation. Typically, the test case specification already defines most of the system states and allows parameter descriptions for a scenario, so there's no point in making everything parameterizable in the test implementation.&lt;br /&gt;
&lt;br /&gt;
==Rules for writing effective Junit test cases==&lt;br /&gt;
=== To Test a method that doesnot return anything ===&lt;br /&gt;
If a method doesn't return a value, it might have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything. &lt;br /&gt;
There may be a way to verify that the side effect actually occurred as expected. For example, consider the add() method in the Collection classes. There are ways of verifying that the side effect happened (i.e. the object was added). You can check the size and assert that it is what is expected:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    public void testCollectionAdd() {&lt;br /&gt;
        Collection collection = new ArrayList();&lt;br /&gt;
        assertEquals(0, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
        assertEquals(1, collection.size());&lt;br /&gt;
        collection.add(&amp;quot;itemB&amp;quot;);&lt;br /&gt;
        assertEquals(2, collection.size());&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Conditions to test get() and set() methods ===&lt;br /&gt;
Unit tests are intended to alleviate fear that something might break. If you think a get() or set() method could reasonably break, or has in fact contributed to a defect, then by all means write a test. &lt;br /&gt;
In short, test until you're confident. What you choose to test is subjective, based on your experiences and confidence level. Remember to be practical and maximize your testing investment. &lt;br /&gt;
&lt;br /&gt;
=== To run setUp() and tearDown() code once for all tests ===&lt;br /&gt;
&lt;br /&gt;
You can add a @BeforeClass annotation to a method to be run before all the tests in a class, and a @AfterClass annotation to a method to be run after all the tests in a class. Here's an example: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
    package junitfaq;&lt;br /&gt;
&lt;br /&gt;
    import org.junit.*;&lt;br /&gt;
    import static org.junit.Assert.*;&lt;br /&gt;
    import java.util.*;&lt;br /&gt;
    &lt;br /&gt;
    public class SimpleTest {&lt;br /&gt;
    &lt;br /&gt;
        private Collection collection;&lt;br /&gt;
	&lt;br /&gt;
        @BeforeClass&lt;br /&gt;
        public static void oneTimeSetUp() {&lt;br /&gt;
            // one-time initialization code        &lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @AfterClass&lt;br /&gt;
        public static void oneTimeTearDown() {&lt;br /&gt;
            // one-time cleanup code&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Before&lt;br /&gt;
        public void setUp() {&lt;br /&gt;
            collection = new ArrayList();&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @After&lt;br /&gt;
        public void tearDown() {&lt;br /&gt;
            collection.clear();&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testEmptyCollection() {&lt;br /&gt;
            assertTrue(collection.isEmpty());&lt;br /&gt;
        }&lt;br /&gt;
	&lt;br /&gt;
        @Test&lt;br /&gt;
        public void testOneItemCollection() {&lt;br /&gt;
            collection.add(&amp;quot;itemA&amp;quot;);&lt;br /&gt;
            assertEquals(1, collection.size());&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&amp;lt;/pre&amp;gt;      &lt;br /&gt;
Given this test, the methods will execute in the following order: &lt;br /&gt;
&lt;br /&gt;
*oneTimeSetUp()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testEmptyCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*setUp()&lt;br /&gt;
*testOneItemCollection()&lt;br /&gt;
*tearDown()&lt;br /&gt;
*oneTimeTearDown()&lt;br /&gt;
&lt;br /&gt;
==Conclusion==&lt;br /&gt;
==References==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-------------------------------------------------------&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=User:Wolverine&amp;diff=18263</id>
		<title>User:Wolverine</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=User:Wolverine&amp;diff=18263"/>
		<updated>2009-09-07T20:57:32Z</updated>

		<summary type="html">&lt;p&gt;Wolverine: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;I am a Graduate Student at NC State University.&amp;lt;br&amp;gt;&lt;br /&gt;
Currently I am pursuing my MS in Computer Engineering and hoping to graduate in May 2010.&lt;/div&gt;</summary>
		<author><name>Wolverine</name></author>
	</entry>
</feed>