title: Mocking Object that returns Java 8 Optional Object with Mockito returns Empty Optional
body: <p>I'm attempting to mock an object that returns a Java Optional object with Mockito:</p>
  
  <pre><code>SomeObject someObject = new SomeObject();
  Mockito.when(someSpringJpaRepository.findById(Mockito.any(UUID.class))).thenReturn(Optional.of(someObject));
  </code></pre>
  
  <p>When <code>someSpringJpaRepository.findById(UUID)</code> is called, it returns <code>Optional.empty</code>.</p>
  
  <p>I'm expecting the method <code>findById()</code> to return <code>someObject</code>.<br>
  Any reason why it's returning <code>Optional.empty</code> instead?</p>
  
  <p>Here is the method:</p>
  
  <pre><code>public UUID saveOrder(OrderDTO orderDto) {
  
          Order order = new Order();
          if (orderDto.getId() != null) {
              Optional&lt;Order&gt; result = orderRepository.findById(orderDto.getId());
              if (result != null &amp;&amp; result.isPresent()) {
                  order = result.get(); // existing record
  
              } else {
                  order.setId(UUID.randomUUID());
              }
          } else {
              // new record
              order.setId(UUID.randomUUID());
          }
  
          return order.getId();
      }
  </code></pre>
  
  <p>And here is the Junit:</p>
  
  <pre><code>@Test
      public void saveOrderTest(){
  
          @Autowired
          private OrderApiService orderApiService;
          @Mock
          private OrderRepository orderRepository;
  
          OrderDTO orderDto = new OrderDTO();
          UUID orderID = UUID.randomUUID();
          Order order = new Order();
          order.setId(orderID);
  
          orderDto.setId(orderID);
          Mockito.when(orderRepository.findById(Mockito.any(UUID.class))).thenReturn(Optional.of(order));
          assertNotNull(orderApiService.saveOrder(orderDto));
      }
  
  </code></pre>