@Test
  public void sendMessageWithAutomaticFunctionCalling_autoRespondToFunctionCall()
      throws IOException, NoSuchMethodException {
    // Arrange
    String message = "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);

    AutomaticFunctionCallingResponder responder = new AutomaticFunctionCallingResponder();
    responder.addCallableFunction("getCurrentWeather", function, "location");

    // Act
    chat = model.startChat();
    GenerateContentResponse response =
        chat.withTools(tools).withAutomaticFunctionCallingResponder(responder).sendMessage(message);

    // Assert
    assertThat(response.getCandidatesList()).hasSize(1);
    // The final response should not contain any function calls since the function was called
    // automatically.
    assertThat(ResponseHandler.getFunctionCalls(response)).isEmpty();

    ImmutableList<Content> history = chat.getHistory();
    Content expectedFunctionResponse =
        ContentMaker.fromMultiModalData(
            PartMaker.fromFunctionResponse(
                "getCurrentWeather", Collections.singletonMap("result", "snowing")));
    assertSizeAndAlternatingRolesInHistory(
        Thread.currentThread().getStackTrace()[1].getMethodName(),
        history,
        4,
        ImmutableList.of(ContentMaker.fromString(message), expectedFunctionResponse));
  }
Callable function getCurrentWeather for testing automatic function calling.
java-vertexai/google-cloud-vertexai/src/test/java/com/google/cloud/vertexai/it/ITChatSessionIntegrationTest.java