This project is a Spring Boot application that demonstrates how to integrate Google's Generative AI (Gemini) into a Java application. It provides a simple REST API to generate stories based on user prompts.
This project has been enhanced with the following features:
- Google Generative AI SDK: Includes the
com.google.genai:google-genai
dependency to interact with the Gemini API. - Story Generation: A new
StoryController
that provides a/story
endpoint to generate text. - Refactored Code: The original
HelloworldController
has been refactored into its own dedicated file. - End-to-End Testing: The project uses Cucumber and Selenium for running browser-based end-to-end tests.
- Java 17: Make sure you have Java 17 or later installed.
- Maven: This project uses Maven for dependency management.
- Google Cloud Project: You need a Google Cloud project with the Vertex AI API enabled.
- API Key: You need to configure your Google Cloud credentials. The simplest way is to use Application Default Credentials by running:
gcloud auth application-default login
- ChromeDriver: For running the integration tests, you need ChromeDriver installed. You can download it from the official site. Make sure the
chromedriver
executable is in your system'sPATH
.
-
Update Project ID: Open
src/main/java/com/example/demo/StoryController.java
and replace"your-google-cloud-project-id"
with your actual Google Cloud project ID. -
Run the service:
mvn spring-boot:run
The application will start on
http://localhost:8080
.
-
Hello World:
curl http://localhost:8080/
This will return "Hello World!".
-
Generate a Story:
curl "http://localhost:8080/story?prompt=Write a short story about a friendly robot"
This will return a story generated by the Gemini model.
To run the full suite of tests, including the Selenium-based browser tests, run:
mvn test
This will execute the Cucumber scenarios, which will open a headless Chrome browser to test the functionality.
You can leverage the integrated Gemini AI to accelerate the process of writing new tests. By providing a clear prompt, you can ask the AI to generate both Cucumber feature files and the corresponding Java step definitions.
For instance, to create a test for the /story
endpoint, you could use a prompt like this:
"Create a new Cucumber test feature file named
story.feature
and the necessary Java step definitions. The test should verify the/story
endpoint by:
- Navigating to the
/story
endpoint with the prompt 'A tale about a brave knight'.- Verifying that the response body contains the phrase 'brave knight'."
The AI might generate a feature file like this:
Feature: Story Generation
Scenario: Generate a story from a prompt
When I navigate to "/story?prompt=A tale about a brave knight"
Then the page content should contain "brave knight"
And the corresponding step definition code to be added to a class in src/test/java/com/example/demo/steps/
:
// Assumes you have a driver instance configured in your step definitions
@When("I navigate to {string}")
public void i_navigate_to(String url) {
driver.get("http://localhost:8080" + url);
}
@Then("the page content should contain {string}")
public void the_page_content_should_contain(String expectedContent) {
String pageSource = driver.getPageSource();
assertTrue("Page content should contain the expected text", pageSource.contains(expectedContent));
}
By using prompts, you can quickly scaffold new tests and focus on the specific logic and assertions, letting the AI handle the boilerplate code.