@Test
  public void sendMessageWithText_throwsIllegalStateExceptionWhenFinishReasonIsNotSTOP()
      throws IOException {
    // (Arrange) Set up the return value of the generateContent
    when(mockGenerativeModel.generateContent(
            Arrays.asList(ContentMaker.fromString(SAMPLE_MESSAGE1))))
        .thenReturn(RESPONSE_FROM_UNARY_CALL_WITH_OTHER_FINISH_REASON);

    // (Act) Send text message via sendMessage
    GenerateContentResponse response = chat.sendMessage(SAMPLE_MESSAGE1);

    // (Act & Assert) get history and assert IllegalStateException for not having the right finish
    // reason.
    IllegalStateException thrown =
        assertThrows(IllegalStateException.class, () -> chat.getHistory());
    assertThat(thrown).hasMessageThat().isEqualTo("Rerun getHistory() to get cleaned history.");
    // Assert that the history can be fetched again and it's empty.
    List<Content> history = chat.getHistory();
    assertThat(history.size()).isEqualTo(0);
  }
Callable function getCurrentWeather for testing automatic function calling.
java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/generativeai/ChatSessionTest.java