Calculator { public void calculate(int lhs, int rhs, char op) { for (var operation : operations) { if (operation.handles(op)) { var result = operation.apply(lhs, rhs); System.out.printf("%d %s %d = %s%n", lhs, op, rhs, result); return; } } throw new IllegalArgumentException("Unknown operation " + op); } Calculator(Collection<Operation> operations); void calculate(int lhs, int rhs, char op);  }
@Test public void shouldCallApplyMethodWhenSuitableOperationFound() { when(mockOperation.handles(anyChar())).thenReturn(true); when(mockOperation.apply(2, 2)).thenReturn(4); calculator.calculate(2, 2, '*'); verify(mockOperation, times(1)) .apply(2,2); }
@Test(expected = IllegalArgumentException.class) public void throwExceptionWhenNoSuitableOperationFound() { when(mockOperation.handles(anyChar())).thenReturn(false); calculator.calculate(2, 2, '*'); }
@Test public void shouldCallApplyMethodWhenSuitableOperationFound() { when(mockOperation.handles(anyChar())).thenReturn(true); when(mockOperation.apply(2, 2)).thenReturn(4); calculator.calculate(2, 2, '*'); verify(mockOperation, times(1)).apply(2,2); }