@Test
  public void sendMessageWithFunctionCalling_functionCallInResponse()
      throws IOException, NoSuchMethodException {
    // Arrange
    String firstMessage = "hello!";
    String secondMessage = "What is the weather in Boston?";

    Method function =
        ITChatSessionIntegrationTest.class.getMethod("getCurrentWeather", String.class);
    Tool tool =
        Tool.newBuilder()
            .addFunctionDeclarations(
                FunctionDeclarationMaker.fromFunc(
                    "Get the current weather in a given location", function, "location"))
            .build();
    ImmutableList<Tool> tools = ImmutableList.of(tool);

    Content functionResponse =
        ContentMaker.fromMultiModalData(
            PartMaker.fromFunctionResponse(
                "getCurrentWeather", Collections.singletonMap("currentWeather", "snowing")));

    // Act
    chat = model.startChat();
    GenerateContentResponse firstResponse = chat.sendMessage(firstMessage);
    GenerateContentResponse secondResponse = chat.withTools(tools).sendMessage(secondMessage);
    GenerateContentResponse thirdResponse = chat.sendMessage(functionResponse);
    ImmutableList<Content> history = chat.getHistory();

    // Assert
    assertThat(firstResponse.getCandidatesList()).hasSize(1);
    assertThat(secondResponse.getCandidatesList()).hasSize(1);
    assertThat(ResponseHandler.getFunctionCalls(secondResponse).size()).isEqualTo(1);
    assertThat(thirdResponse.getCandidatesList()).hasSize(1);
    assertSizeAndAlternatingRolesInHistory(
        Thread.currentThread().getStackTrace()[1].getMethodName(),
        history,
        6,
        ImmutableList.of(
            ContentMaker.fromString(firstMessage),
            ContentMaker.fromString(secondMessage),
            functionResponse));
  }
Callable function getCurrentWeather for testing automatic function calling.
java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/it/ITChatSessionIntegrationTest.java