Skip to content

mdebowczyk/AI-cucumber-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Java and Gemini API Starter Project

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.

Getting Started

Prerequisites

  • 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's PATH.

Running the Application

  1. 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.

  2. Run the service:

    mvn spring-boot:run

    The application will start on http://localhost:8080.

Using the API Endpoints

  • 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.

Running the Tests

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.

Using Generative AI to Create Tests

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.

Example Prompt

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:

  1. Navigating to the /story endpoint with the prompt 'A tale about a brave knight'.
  2. Verifying that the response body contains the phrase 'brave knight'."

Generated Feature File (src/test/resources/com/example/demo/story.feature)

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"

Generated Step Definitions (Java)

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published