This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Testing and the fly likes How to test method without no return value by using JUnit. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "How to test method without no return value by using JUnit." Watch "How to test method without no return value by using JUnit." New topic
Author

How to test method without no return value by using JUnit.

jami siva
Ranch Hand

Joined: Oct 16, 2009
Posts: 63
How to test method without no return value by using JUnit.

void add(int a, int b) {
int c= a + b;
}
How to test above method by using Junit in Java.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4782
    
    8

First, you need to find something interesting about this method. A method worth testing is a method that does something useful that can be verified from outside of the method. This code does not appear to have either of those attributes, making it pointless to unit test.


Junilu - [How to Ask Questions] [How to Answer Questions]
Marshall Blythe
Ranch Hand

Joined: Feb 26, 2013
Posts: 32
Testing a method requires you to either make assertions about a returned value or make assertions about the method's side effects (or possibly both). If a void method has no side effects (as your example illustrates) then it isn't doing anything useful and isn't worth testing. The general approach to testing void methods is to make assertions on the side effects. This often involves making assertions about the way the method interacts with collaborating classes. Mock Objects can be very useful in this case, but you need to design your classes in such a way that the mock objects can be easily substituted for the real objects at test time.
paul nisset
Ranch Hand

Joined: May 13, 2009
Posts: 174
Assuming ,as other have said, that your method does something, you could output to a log file from within the test method .
You would then open the file from within the test method,read the last entry, and test that it matches what you would expect.

The main goal is to be clear on what the method is trying to achieve and test that goal .
If it's an external resource like a file,you have to mock/mimic that resource from with your test.

Use set up and tear down methods to create and remove resources that your test uses.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Jami

Further to what Junilu has said, you would never have a method like this in a project (it doesn't do anything) so asking for examples of testing it is futile.

Some good techniques have been given here for unit testing viable void methods so I hope that has been helpful to you.
 
Consider Paul's rocket mass heater.
 
subject: How to test method without no return value by using JUnit.