Consider a class that uses an external jar. The class processes objects of type D, which are obtained via objects A, B, and C, all of which external objects from the jar.
class DProcessor() { public void process(PoolOfA pool) { A a = pool.borrowObject() ... B b = a.getB() C c = b.getC() for (D d : c.getAllDs()) { // Do something meaningful with d } } } How do I Unit test process(PoolOfA pool)?
My best shot so far is writing mocks for all external classes:
PoolOfA pool = mock(PoolOfA.class); A a = mock(A.class); B b = mock(B.class); C c = mock(C.class); D d1 = mock(D.class); D d2 = mock(D.class); D d3 = mock(D.class); D d4 = mock(D.class); List listOfDs = new ArrayList<D>(); listOfDs.add(d1); listOfDs.add(d2); listOfDs.add(d3); listOfDs.add(d4); // Set specific behaviour for each d when(pool.borrowObject()).thenReturn(a); when(b.getC()).thenReturn(a); when(c.getAllDs()).thenReturn(d); when(b.getC()).thenReturn(c); when(c.getAllDs()).thenReturn(listOfDs); This seems cumbersome and inelegant. Is there a better way?