Testing void methods

Testing void methods

Postby Laxmi » Wed Mar 06, 2013 9:55 pm

How to write JUNIT test for testing void methods?
Laxmi
 
Posts: 25
Joined: Thu Jan 31, 2013 8:45 pm

Re: Testing void methods

Postby Kishan » Thu Mar 14, 2013 12:14 pm

Good coding practice is every line of application code we are going to write should be testable. If any code is not testable means definitely code can be improved.

Example for class with void method: removeDuplicates method will remove duplicate values from give list of values.

Code: Select all
public class DataUtils {
   public static void removeDuplicates(List<String> list) {
        //here you can write a logic to remove duplicate values from given list.
   }
}



Test Class for above removeDuplicates method:

Code: Select all
public class DataUtilsTest {
   @Test
    public void testRemoveDuplicates() {
        List<String> list = new ArrayList<String>();
        list.add("John");
        list.add("Dave");
        list.add("John");
        DataUtilis.removeDuplicates(list);
        Assert.assertEquals(2, list.size());
        Assert.assertEquals(1, Collections.frequency(list, "John"));
    }
}
Kishan
 
Posts: 191
Joined: Tue Jan 29, 2013 4:21 am


Return to JUNIT

Who is online

Users browsing this forum: No registered users and 1 guest

cron