title: why mocking mongotemplate.aggregate() returns NullPointerException [Spring Boot Mockito]
body: <p>How to mock <code>mongoTemplate.aggregation().getUniqueMappedResults()</code>?</p>
  
  <p>Why does the following return <code>NullPointerException</code> in <code>RepoClass</code>?</p>
  
  <pre><code>mongoTemplate.aggregate(aggregation, COLLECTION_NAME,ScreenDetailsEntity.class);
  </code></pre>
  
  <p>I am new to Spring and writing test cases first time. I am highly tired.
  Please help. Thanks in advance.</p>
  
  <p><code>RepoTestClass</code>'s <code>Test</code> function:</p>
  
  <pre><code>  @Test
    public void testValidGetScreenDetails() {
      ReflectionTestUtils.setField(
          screenDetailsRepoService, "screenFields",
          ClassUtil.getFieldName(ScreenDetailsEntity.class));
      Aggregation aggregation1 = Aggregation.newAggregation(
          match(where(PAGE).is("Bookings")),
          project(ClassUtil.getFieldName(ScreenDetailsEntity.class)),
          limit(1)
      );
  
      MongoTemplate mongoTemplate = Mockito.mock(MongoTemplate.class);
      Aggregation aggregation = Mockito.mock(Aggregation.class);
      ScreenDetailsEntity screenDetailsEntity = Mockito.mock(ScreenDetailsEntity.class);
      AggregationResults&lt;ScreenDetailsEntity&gt; aggregationResultsMock = Mockito
          .mock(AggregationResults.class);
      Mockito.when(aggregationResultsMock.toString()).thenReturn(new String());
      Mockito.doReturn(aggregationResultsMock).when(mongoTemplate).aggregate(aggregation1, COLLECTION_NAME, ScreenDetailsEntity.class);
  
      Mockito.doReturn(screenDetailsEntity).when(aggregationResultsMock).getUniqueMappedResult();
      screenDetailsRepoService.getScreenDetails(
          "Bookings", "Web8", "List/View", "en", 45429, 121);
    }
  </code></pre>
  
  <p>RepoClass:</p>
  
  <pre><code>public class ScreenDetailsRepoService {
  
    @Autowired
    private MongoTemplate mongoTemplate;
  
    private String[] screenFields;
  
    @PostConstruct
    public void init() {
      screenFields = ClassUtil.getFieldName(DetailsEntity.class);
    }
  
    public ScreenDetailsEntity getScreenDetails(final @NonNull String page,
        final @NonNull String client, final String module,
        final String locale, final Integer hotelId, final Integer countryId
    ) throws NoDataFoundException {
      ScreenDetailsEntity screenDetailsEntity;
      Aggregation aggregation = newAggregation(
          match(where(PAGE).is(page)),
          project(screenFields),
          limit(1)
      );
      long t1 = System.currentTimeMillis();
      screenDetailsEntity = mongoTemplate.aggregate(aggregation, COLLECTION_NAME, ScreenDetailsEntity.class).getUniqueMappedResult();
      log.info(
          "DB Query: ScreenDetailsRepoService : getScreenDetails - Time taken for db requests : {} milliseconds",
          (System.currentTimeMillis() - t1));
      if (screenDetailsEntity == null) {
        log.error(
            "ScreenDetailsRepoService::getScreenDetails - No data found for for page {} ,module {}, locale {}, hotel {}, country {}",
            page, module, locale, hotelId, countryId);
        throw new NoDataFoundException("No data found");
      }
      return screenDetailsEntity;
    }
  </code></pre>