title: How to mock a query inside a function with Mockito
body: <p>I'm writing a Junit test, but some of the test depends of specific data from the database, so my plan is to use Mockito to not depend on the db, but I'm having problem with some functions that contain queries, using MongoMorphia.</p>
  <p>This is the test:</p>
  <pre><code>@Test
      public void testExceptionIDisValid() throws BusinessException {
          Receptor = new Receptor();
          receptor.setTipoEspecial(null);
          receptor.setIDReceptor(&quot;12521357&quot;);
          validationIDReceiver.validateReceiver(receptor);
      } 
  </code></pre>
  <p>The function:</p>
  <pre><code>private static Datastore datastore = MongoMorphia.getDatastore();
  
  public void validateReceiver(Receptor receiver) throws BusinessException {
          if (!receiver.getIDReceptor().equals(&quot;CF&quot;)) {
              final Query&lt;MiniRTU&gt; query = datastore.find(MiniRTU.class).filter(&quot;nit&quot;, receiver.getIDReceptor());
              Optional&lt;MiniRTU&gt; result = Optional.ofNullable(query.get());
              if (result.isEmpty()) {
                  throw new BusinessException(NIT_RECEIVER_INVALID);
              }
          }
  }
  </code></pre>
  <p>I try doing something like this with mockito:</p>
  <pre><code>public class TestIDReceiver {
  
      private IngestorConfiguration configuration;
      private ValidationIDReceiver validationIDReceiver;
      Datastore ds = mock(Datastore.class);
      private static final String nit = &quot;12521357&quot;;
  
      @Before
      public void setup() throws IOException, ConfigurationException, Exception {
          final ObjectMapper objectMapper = Jackson.newObjectMapper();
          final Validator validator = Validators.newValidator();
          final YamlConfigurationFactory&lt;IngestorConfiguration&gt; factory = new YamlConfigurationFactory&lt;&gt;(IngestorConfiguration.class, validator, objectMapper, &quot;dw&quot;);
          final File yaml = new File(Thread.currentThread().getContextClassLoader().getResource(&quot;config.yml&quot;).getPath());
          configuration = factory.build(yaml);
          MongoMorphia.create(configuration);
          validationIDReceiver = ValidationIDReceiver.getInstance(configuration);
      }
  
      @Test
      public void testExceptionIDNitIsValid() throws BusinessException {
          Receptor receptor = new Receptor();
          FieldEnd fieldEnd = mock(FieldEnd.class);
          MiniRTU mini = new MiniRTU();
          mini.setNit(nit);
          receptor.setTipoEspecial(null);
          receptor.setIDReceptor(&quot;12521357&quot;);
          Query&lt;MiniRTU&gt; q = mock(Query.class);
          
          when(fieldEnd.equal(receptor)).thenReturn(q);
          when(q.get()).thenReturn(mini);
          when(ds.find(MiniRTU.class)).thenReturn(q);
          when(ds.find(MiniRTU.class).field(&quot;nit&quot;)).thenReturn(fieldEnd);
          when(fieldEnd.exists()).thenReturn(receptor);
  //        when(ds.find(MiniRTU.class)).thenReturn(q);
  //        when(ds.find(MiniRTU.class).field(&quot;nit&quot;)).thenReturn(fieldEnd);
  //        when(ds.find(MiniRTU.class).field(&quot;nit&quot;).equal(mini)).thenReturn(q);
  //        when(q.get()).thenReturn(mini);
  //        when(ds.find(MiniRTU.class).field(&quot;nit&quot;)).thenReturn(q);
          validationIDReceiver.validateReceiver(receptor);
      }
  
  }
  </code></pre>
  <p>I left the commented part, because it's something I also tried with.</p>
  <p>As you can see, I want to try to mock the query inside of the function.</p>
  <pre><code>final Query&lt;MiniRTU&gt; query = datastore.find(MiniRTU.class).filter(&quot;nit&quot;, receiver.getIDReceptor());
              Optional&lt;MiniRTU&gt; result = Optional.ofNullable(query.get());
  </code></pre>
  <p>But so far with what I have tried, it does not send any data, only null, so in the function, it goes to the part of <code>if (result.isEmpty())</code> and throw the exception. Any ideas of what can I do ?</p>