@Test
public void testGetNestedAggregationSubAggregationIsNull()
{

    // Create mock nested aggregations.
    Aggregations nestedAggregations = mock(Aggregations.class);
    when(nestedAggregations.get(SUB_AGGREGATION_NAME)).thenReturn(null);
    // Create a mock nested aggregation.
    Nested nestedAggregation = mock(Nested.class);
    when(nestedAggregation.getAggregations()).thenReturn(nestedAggregations);
    // Create mock search response aggregations.
    Aggregations searchResponseAggregations = mock(Aggregations.class);
    when(searchResponseAggregations.get(NESTED_AGGREGATION_NAME)).thenReturn(nestedAggregation);
    // Create a mock search response.
    SearchResponse searchResponse = mock(SearchResponse.class);
    when(searchResponse.getAggregations()).thenReturn(searchResponseAggregations);
    // Mock the external calls.
    when(jsonHelper.objectToJson(searchResponse)).thenReturn(SEARCH_RESPONSE_JSON_STRING);
    when(jsonHelper.objectToJson(nestedAggregation)).thenReturn(NESTED_AGGREGATION_JSON_STRING);
    // Try to call the method under test.
    try
    {
        elasticsearchHelper.getNestedAggregation(searchResponse, NESTED_AGGREGATION_NAME, SUB_AGGREGATION_NAME);
        fail();
    }
    catch (IllegalStateException e)
    {
        assertEquals("Invalid search result.", e.getMessage());
    }
    // Verify the external calls.
    verify(jsonHelper).objectToJson(searchResponse);
    verify(jsonHelper).objectToJson(nestedAggregation);
    verifyNoMoreInteractionsHelper();
}