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

M

mohit.khatri28

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
 
I

Ingo R. Homann

Hi,

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.

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
 
D

Daniel Pitts

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

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.
 
M

mohit.khatri28

Hi,





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

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
 
I

Ingo R. Homann

Hi,

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.

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
 
P

Patricia Shanahan

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.

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
 
D

Daniel Pitts

Hi,












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- Hide quoted text -

- 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.
 
P

Patricia Shanahan

Ingo said:
Hi,



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.)

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
 
N

news.rcn.com

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()
 
M

mohit.khatri28

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
 
P

Patricia Shanahan

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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top