Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

How to test a method which has void return type and no argument?

Thread view: 
mohit.khatri28@gmail.com - 18 Apr 2007 10:43 GMT
Hi All,

  I am using JUnit for testing java application under Eclipse IDE. I
have a method which has void return type and no arguments i.e.

void xyz()
{
    ..............

}

  Could anybody suggest me how do i test this type of method using
JUnit.

Thanks & Regards
Mohit
Ingo R. Homann - 18 Apr 2007 12:02 GMT
Hi,

> Hi All,
>
[quoted text clipped - 9 lines]
>    Could anybody suggest me how do i test this type of method using
> JUnit.

well, of course you can only test this kind of method, if it does
something that can be "seen" by the Program. e.g.:

void test() {
  MyObject o=new MyObject();
  assertEquals(o.getFoo(),"foo");
  o.setFooToBar();
  assertEquals(o.getFoo(),"bar");
}

If the method does *nothing* at all, it cannot be tested.

Of course, it may be possible that it is a bit more difficult than shown
above, to "read" the result. But that depends on your use-case.

Ciao,
Ingo
mohit.khatri28@gmail.com - 19 Apr 2007 11:39 GMT
> Hi,
>
[quoted text clipped - 31 lines]
> Ciao,
> Ingo

Hi,

  Thanks for your reply. I am facing still a problem. Suppose if i
have a method i.e.

class Menu_Bar
{

JMenuBar menubar = new JMenuBar();
JMenuItem File;
JFrame frame = new JFrame();

public Menu_Bar()
{
   this.CreateMenu();
   ...
   frame.setJMenuBar(menubar);
}

void CreateMenu()
{
    JMenu menuFile = new JMenu("File");
    ......

}
}

Now could you suggest me how do i test this method i.e. CreateMenu()
which has some local components.

Thanks & Regards
Mohit
Ingo R. Homann - 19 Apr 2007 12:37 GMT
Hi,

> Hi,
>
[quoted text clipped - 25 lines]
> Now could you suggest me how do i test this method i.e. CreateMenu()
> which has some local components.

Sorry, but I think it is nearly impossible to write unit tests that
tests a GUI! (Well, indeed it is possible, but it is quite difficult and
never covers what a manual test covers.)

Ciao,
Ingo
Daniel Pitts - 19 Apr 2007 17:04 GMT
> Hi,
>
[quoted text clipped - 37 lines]
>
> - Show quoted text -

Like Patricia has stated, JUnit tests don't work well for GUI's.

JUnit is best for testing functionality use cases, rather than
usability.

Write tests for each of the user actions, not for the gui :-)

Hope this helps,
Daniel.
Patricia Shanahan - 19 Apr 2007 17:10 GMT
> Hi,
>
[quoted text clipped - 31 lines]
> tests a GUI! (Well, indeed it is possible, but it is quite difficult and
> never covers what a manual test covers.)

I completely agree about testing a GUI, but I don't see any reason not
to unit test methods that contribute to a GUI.

Patricia
Patricia Shanahan - 19 Apr 2007 13:31 GMT
>> Hi,
>>
[quoted text clipped - 56 lines]
> Now could you suggest me how do i test this method i.e. CreateMenu()
> which has some local components.

The first thing I would do is to add Javadoc comments or equivalent to
CreateMenu(). The really serious problem with unit testing it is that
the program contains no statements about its preconditions and
postconditions.

You then have to ensure two things, that CreateMenu's unit test has the
power to make the preconditions true, and has access to enough data to
check the postconditions. For example, if there are postconditions
related to menubar you will need a getMenuBar method returning its
current value.

The form of the unit test is:

Make the method's preconditions true.
Call the method.
Get the values of variables involved in its postconditions.
Test the postcondition assertions.

Patricia
news.rcn.com - 19 Apr 2007 21:31 GMT
What 'side effects' does this method have that you are interested in?  If
there are none, then it just runs some code and quits and no-one would care
if it did that correctly or not.
There's no return code, so that's not a side-effect?
Does it set any class or global (hopfully not) variables?  check those.
Do you want to check the menu for validity?  have the method return it and
test it.

From what you've shown us of your method, there's really not much
interesting going on.

Sorry if this sounds cynical,
jim

p.s. As others have said, JUnit's not really up to Gui testing - (though you
could create GUI objects and exercise them with through code if you want to
bother to program the user actions (clicks, text input, etc.).

void CreateMenu()
> {
>     JMenu menuFile = new JMenu("File");
>     ......
> }

>> Hi,
>>
[quoted text clipped - 64 lines]
> Thanks & Regards
> Mohit
mohit.khatri28@gmail.com - 23 Apr 2007 05:08 GMT
Hi,

  First of all Thanks to all for suggesting me valuable solution.

  Now i would like to ask that my application has been integrated. I
want to test unit testing of my application. Is it possible to test
unit testing after integration of code. If i use JFCUnit i.e. an
extension of JUnit then it is possible to test my methods which
neither returns any value nor contains any argument. Or manual testing
is better solution for testing my GUI application. Please suggest me a
solution.

Thanks & Regards
Mohit
Patricia Shanahan - 24 Apr 2007 11:40 GMT
> Hi,
>
[quoted text clipped - 7 lines]
> is better solution for testing my GUI application. Please suggest me a
> solution.

I find this question confusing, because it seems to me to suggest as
alternatives two forms of testing I consider complementary:

1. Unit testing individual classes, and methods within classes. Tests
whether a unit conforms to its contract with the rest of the application.

2. System test a complete application. Tests the external behavior of
the whole application.

I don't see how they can be alternatives, to be compared. Each has its
own purpose. It is often difficult, or even impossible, to test the
entire contract for a unit by manipulating only external inputs and
outputs. On the other hand, testing that each unit conforms to its
contract does not necessarily ensure the application behaves sensibly.

Patricia
Daniel Pitts - 18 Apr 2007 16:03 GMT
On Apr 18, 2:43 am, mohit.khatr...@gmail.com wrote:
> Hi All,
>
[quoted text clipped - 12 lines]
> Thanks & Regards
> Mohit

Generally, you should write test cases that assert the expected side
effects of your method.

Ingo has provided a simple example there of.

Usually a good JUnit test looks like:

1. Set up precondition for test
2. Optionally assert precondition. In case there is a problem setting
up the precondition.
3. Emulate a Use Case for your code. (Possibly using Mock Objects,
etc...)
4. Assert postcondition.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.