lundi 29 juin 2015

Code to test in Isolation using Mockito and Junit

I have basic understanding of how to apply Mockito framework. But when it comes to some real time scenarios I failed to write tests in Isolation(by Mocking the dependent classes).

Can you help me to write Unit test for PriorityQueuePrinter class by Mocking PriorityQueue Implementation(BinaryMaxHeap.java)

In short, Priority Queue is the Implementation for BinaryHeapTree and Printer class uses Priority Queue.

Below are the code classes.

public interface PriorityQueue<T extends Comparable<T>> {
int size();
void insert(T element);
T popMax();

}

public class BinaryMaxHeap<T extends Comparable<T>> implements PriorityQueue<T> {

private ArrayList<T> items;

public BinaryMaxHeap() {
    items = new ArrayList<T>();
}   

public int size() {
    return items.size();
}

public void insert(T element) {
    items.add(element);
    shiftUp();
}

public T popMax() {
    if (items.size() == 1) {
        return items.remove(0);
    }
    T hold = items.get(0);
    items.set(0, items.remove(items.size()-1));
    shiftDown();
    return hold;
}

/*
 * place newly added element in correct position in binary tree
 */
private void shiftUp() {
    int k = items.size() - 1;
    while (k > 0) {
        int p = (k-1) / 2;  // get parent element index
        T child = items.get(k);
        T parent = items.get(p);
        if (child.compareTo(parent) > 0) {
            // parent and child are not in correct position, need to swap
            items.set(k, parent);
            items.set(p, child);
            k = p;
        } else {
            break;
        }
    }
}

private void shiftDown() {
    int k = 0;
    int l = 2*k+1;  // left leaf node
    while (l < items.size()) {
        int max = l;    // assume left node as max element
        int r = l+1;    // right leaf node
        if (r < items.size()) { 
            if (items.get(r).compareTo(items.get(l)) > 0) { 
                max++;  // identify max element in leaf nodes
            }
        }
        T parent = items.get(k);
        T child = items.get(max);
        if (parent.compareTo(child) < 0) {
            // parent element is less than leaf node, need to swap it
                T temp = items.get(k);
                items.set(k, items.get(max));
                items.set(max, temp);
                k = max;
                l = 2*k+1;
        } else {
            break;
        }
    }
}



   public interface Printer {
public <T extends Comparable<T>> String asSortedString(T... values);

}

public class PriorityQueuePrinter implements Printer {

private PriorityQueue priorityQueue = null;

public <T extends Comparable<T>> PriorityQueuePrinter(PriorityQueue<T> priorityQueue) {
    this.priorityQueue = priorityQueue;
}

public <T extends Comparable<T>> String asSortedString(T... values) {

    //PriorityQueue<T> priorityQueue = 
    addElements(values);
    //return getSortedElements();
    return null;
}

private <T extends Comparable<T>> void addElements(T... values) {
    //PriorityQueue<T> priorityQueue = new BinaryMaxHeap<T>();
    for (T element : values) {
        priorityQueue.insert(element);
    }
    //return priorityQueue;
}

public int size() {
    return priorityQueue.size();
}

private String getSortedElements() {
    StringBuilder sortedElements =  new StringBuilder();
    boolean isFirstElement = true;
    while(priorityQueue.size() > 0) {
        if (!isFirstElement) {
            sortedElements.append(",");
        }
        isFirstElement = false;
        sortedElements.append(priorityQueue.popMax());
    }
    return sortedElements.toString();
}

public static void main(String a[]) {
    PriorityQueuePrinter p = new PriorityQueuePrinter(new BinaryMaxHeap<Integer>());
    String sortedElements = p.asSortedString(1,4,6,3,2);
    System.out.println(sortedElements);
}

}

Below is the sample test code tried but not able to complete.

public class PrinterTest {

@Mock
PriorityQueue<Integer> mockPriorityQueue;   // mock object
//PriorityQueue<Integer> priorityQueue = new BinaryMaxHeap<Integer>();

@Test
public void testPriorityQueue() {
    PriorityQueuePrinter printer = new PriorityQueuePrinter(mockPriorityQueue);
    System.out.println(mockPriorityQueue);
    Mockito.when(mockPriorityQueue.size()).thenReturn(2);

    Integer[] intarray = {1,3};
    printer.asSortedString(intarray);

    for (int i : intarray)
    {
        Mockito.verify(mockPriorityQueue).insert(Matchers.eq(i));
    }
}

@Before
public void setUp() throws Exception {
    //mockPriorityQueue = new BinaryMaxHeap<Integer>();
    MockitoAnnotations.initMocks(this);
}

}

Aucun commentaire:

Enregistrer un commentaire