You nailed it: you mocked testImage and call the layout method, while stubbing is not allowed. The documentation is not clear on this limitation.
I wrote a comprehensive answer to Mockito's layouts before (see "implementation details"), but the short version is this: Mockito stores its arguments on the stack and that every call to the n-argument bullying method checks that the stack contains exactly 0 or n matches.
Mockito.verify(drawingUtil).drawTextOnCanvas( Matchers.eq(imageCategory.getWidth()), // A1 Matchers.eq(mockGraphics), // A2 Matchers.any(Font.class), // A3 Matchers.eq(Arrays.asList("Test text")), // A4 Matchers.eq(testImage.getHeight() + 10), // B Matchers.any(FontMetrics.class), // C1 Matchers.eq(10)); // C2
The order of the call looks something like this: After verify(drawingUtil) Java prepares the drawTextOnCanvas arguments in order, calling A1 to A4. Suddenly a call to B occurs, and Mockito prepares, as if textImage.getHeight() is embedded in the when(...) call. There are 4 responders on the stack, and Mockito expects either equality matches (null matches) or the number of getHeight() arguments (also null matches). Mockito throws this exception with this "4 branches" message, without even getting C1, C2 or drawTextOnCanvas .
As a good practice, Matchers.eq all Matchers.eq values (containing method calls) by local variables, with particular emphasis on calls to mocking objects.
source share