Tuesday, January 14, 2014

Unit Testing Example with Code Coverage using Cobertura

Cobertura Coverage in NetBeans 7.4
I gave a presentation on unit testing a few months ago at Greenville Java Users Group (GreenJUG) about unit testing using JUnit and Cobertura. I finally have gotten around to publishing the code with a little cleanup.

NetBeans will detect the addition of Cobertura in the POM file and automatically add menu items for checking code coverage. This is a very slick feature in NetBeans.

The Cobertura coverage shows 90% in the project because it does not handle the generics correctly at this time.

The Apache Maven based project can be downloaded from Bitbucket below:

unit-testing-demo

Here is an example snippet of the code.

ListDataStoreImplTest.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.bluelotussoftware.junit;
 
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
 
/**
 *
 * @author John Yeary
 * @version 1.0
 */
public class ListDataStoreImplTest {
 
    public ListDataStoreImplTest() {
    }
 
    @Test
    public void testConstuctor() {
        System.out.println("Constructor test");
        List<string> list = new ArrayList<string>() {
            private static final long serialVersionUID = 3109256773218160485L;
 
            {
                add("Hello");
                add("World");
            }
        };
 
        ListDataStore<string> instance = new ListDataStoreImpl(list);
        assertEquals(list.get(0), instance.getData().get(0));
        assertEquals(list.get(1), instance.getData().get(1));
        assertEquals(list, instance.getData());
        assertEquals(2, instance.getData().size());
    }
 
    @Test
    public void objectTest() {
        System.out.println("Generic Object test");
        List list = new ArrayList() {
            private static final long serialVersionUID = 3109256773218160485L;
 
            {
                add("Hello");
                add("World");
            }
        };
 
        ListDataStore instance = new ListDataStoreImpl(list);
        assertEquals(list.get(0), instance.getData().get(0));
        assertEquals(list.get(1), instance.getData().get(1));
        assertEquals(list, instance.getData());
        assertEquals(2, instance.getData().size());
    }
 
    /**
     * Test of getData method, of class ListDataStoreImpl.
     */
    @Test
    public void testGetData() {
        // Check #1 - Check the new datastore.
        System.out.println("getData");
        ListDataStoreImpl instance = new ListDataStoreImpl();
        List expResult = new ArrayList();
        List result = instance.getData();
        assertEquals(expResult, result);
 
        // Check #2 - Check the list. Mockito is overkill, but works.
        instance.add("ABC");
        List l = mock(List.class);
        when(l.get(0)).thenReturn("ABC");
        assertEquals(l.get(0), instance.getData().get(0));
 
        // Check #3 - Check to make sure that the list size is correct.
        assertEquals(1, instance.getData().size());
    }
 
    /**
     * Test of add method, of class ListDataStoreImpl.
     */
    @Test
    public void testAdd() {
        System.out.println("add");
        String element = "ABC";
        ListDataStoreImpl instance = new ListDataStoreImpl();
        boolean expResult = true;
        boolean result = instance.add(element);
        assertEquals(expResult, result);
    }
 
    /**
     * Test of remove method, of class ListDataStoreImpl.
     */
    @Test
    public void testRemoveEmpty() {
        System.out.println("remove");
        String element = "ABC";
        ListDataStoreImpl instance = new ListDataStoreImpl();
        boolean expResult = false;
        boolean result = instance.remove(element);
        assertEquals(expResult, result);
    }
 
    @Test
    public void testRemove() {
        System.out.println("remove");
        String element = "ABC";
        ListDataStoreImpl instance = new ListDataStoreImpl();
        instance.add(element);
        boolean expResult = true;
        boolean result = instance.remove(element);
        assertEquals(expResult, result);
    }
}

0 comments :

Popular Posts