使用Mockito在隔离中进行JUnit测试

时间:2015-06-29 09:38:16

标签: java unit-testing junit mockito

我对如何应用Mockito框架有基本的了解。 但是当谈到一些实时场景时,我无法在隔离中编写测试(通过模拟依赖类)。

您能否通过Mocking PriorityQueue Implementation(BinaryMaxHeap.java)帮助我为PriorityQueuePrinter类编写单元测试。

我使用BinaryMaxHeap对象编写了testPriorityQueue(),在这种情况下,我的测试成功但我希望实现相同的模拟BinaryMaxHeap,以便我的测试将是Isolate。我想我也必须在我的测试方法中设置方法行为。

简而言之,Priority Queue是BinaryHeapTree的实现,而Printer类使用Priority Queue。

以下是代码类。

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);
    }
}

以下是已尝试但未能完成的示例测试代码。

public class PrinterTest {
    @Mock
PriorityQueue<Integer> mockPriorityQueue;   // mock object
PriorityQueue<Integer> priorityQueue;

@Test
public void testPriorityQueueWithMock() {

    PriorityQueuePrinter printer = new PriorityQueuePrinter(mockPriorityQueue);
    String s = printer.asSortedString(5,3,6);
    assertEquals("6,5,3", s);
}

@Ignore
public void testPriorityQueue() {
    priorityQueue = new BinaryMaxHeap<Integer>();
    PriorityQueuePrinter printer = new PriorityQueuePrinter(priorityQueue);

    String s = printer.asSortedString(5,3,6);

    assertEquals("6,5,3", s);
}

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
}

@After
public void tearDown() throws Exception {
    System.out.println("==tearDown==");
}
    }

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

0 个答案:

没有答案