how to test a method which is not returning any value ?
1 posts in topic
Flat View  Flat View

Posted By:   Anonymous
Posted On:   Sunday, March 31, 2002 09:54 PM

A method might be returning some value or might not be. what technique has to be used to test later one.

Re: how to test a method which is not returning any value ?

Posted By:   Dave_Astels  
Posted On:   Monday, April 1, 2002 08:24 AM

Often if a method doesn't return a value, it will have some side effect. Actually, if it doesn't return a value AND doesn't have a side effect, it isn't doing anything.

So, I assume it has a side effect. There may be a way to verify that the side effect actually occured & ocurred 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:

public void testAdd() {
aCollection = new SomeCollectionClass();
assertEquals(0, aCollection.size());
assertEquals(1, aCollection.size());
assertEquals(2, aCollection.size());

Another approach is to make use of a concept called MockObjects (see www.mockobjects.com). This a big topic, so I'll let you explore the site.

A related issue is to design for testing. For example, if you have a method that is meant to output to a file, don't pass in a filename, or even a FileWriter. Pass in a Writer. That way you can pass in a StringWriter to capture the output for testing purposes. Then you can add a method (e.g. writeToFileNamed(String filename)) to encapsulate the FileWriter creation.


About | Sitemap | Contact