@Test
  public void sendMessageStreamWithText_throwsIllegalStateExceptionIfResponseNotConsumed()
      throws IOException {
    // (Arrange) Set up the responseStream
    ResponseStream<GenerateContentResponse> responseStream =
        new ResponseStream(new ResponseStreamIteratorWithHistory(mockServerStreamIterator));

    // (Arrange) Set up the return value of the generateContentStream
    when(mockGenerativeModel.generateContentStream(
            Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))))
        .thenReturn(responseStream);

    // (Act & Assert) Send request, consume response and get history, but not consume the response
    // Assert IllegalStateException will be thrown.
    ResponseStream<GenerateContentResponse> returnedResponseStream =
        chat.sendMessageStream(SAMPLE_MESSAGE1);

    IllegalStateException thrown =
        assertThrows(IllegalStateException.class, () -> chat.getHistory());
    assertThat(thrown).hasMessageThat().isEqualTo("Response stream is not consumed");
  }
Callable function getCurrentWeather for testing automatic function calling.
java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java