Generate Java unit tests

Generate unit tests for legacy Java code

You can query a model directly and test the results returned when using different parameter values with the Cloud console, or by calling the Vertex AI API directly.

System instructions

You are a proficient Java developer and an expert in Test-Driven development. Your primary goal is to write clean, efficient, and maintainable Java code and to ensure that all functionalities are thoroughly tested.

Prompt Freeform

I am a Java developer and I have been following the test-driven development methodology. I have the following code that is used to operate a robot, which I am going to refactor soon to add the cleaning functionality. As a way to ensure that I do not break anything during the refactoring process and to understand the behavior of the code, I want to add some characterization tests to the existing code.

  • Refer to the following code and write unit tests using JUnit 4 for the methods in the RoboCleaner class.
  • Please make sure that you provide meaningful names to the tests, and cover any edge cases.
public class RoboCleaner {
int x, y;
Direction direction;

public RoboCleaner(int initialX, int initialY, Direction initialDirection) {
this.x = initialX;
this.y = initialY;
this.direction = initialDirection;
}

public void moveForward(int steps) {
switch (direction) {
case NORTH:
y += steps;
break;
case SOUTH:
y -= steps;
break;
case EAST:
x += steps;
break;
case WEST:
x -= steps;
break;
}
}

public void turnLeft() {
direction = direction.turnLeft();
}

public void turnRight() {
direction = direction.turnRight();
}

public String getPosition() {
return String.format("Position: (%d, %d), Facing: %s", x, y, direction);
}

public enum Direction {
NORTH, EAST, SOUTH, WEST;

private static final Direction[] leftTurns = {WEST, NORTH, EAST, SOUTH};
private static final Direction[] rightTurns = {EAST, SOUTH, WEST, NORTH};

public Direction turnLeft() {
return leftTurns[ordinal()];
}

public Direction turnRight() {
return rightTurns[ordinal()];
}
}
}

Response

Of course! As a fellow Java developer and TDD enthusiast, I'd be happy to help you write a comprehensive set of characterization tests for your RoboCleaner class.

Characterization tests are an excellent practice. They capture the current behavior of the code, creating a safety net that ensures your refactoring doesn't introduce unintended side effects.

Here are the unit tests for the RoboCleaner class using JUnit 4. I've focused on creating descriptive test names following the given[State]_when[Action]_then[ExpectedResult] pattern, which makes the purpose of each test immediately clear.

RoboCleanerTest.java

import org.junit.Test;
import static org.junit.Assert.assertEquals;

/**
 * Characterization tests for the RoboCleaner class.
 * These tests document and verify the existing behavior of the class
 * to ensure that future refactoring does not introduce regressions.
 */
public class RoboCleanerTest {

    // --- Constructor and Initial State Tests ---

    @Test
    public void whenRoboCleanerIsInitialized_thenItsPositionIsSetCorrectly() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(10, 20, RoboCleaner.Direction.SOUTH);
        String expectedPosition = "Position: (10, 20), Facing: SOUTH";

        // Act
        String actualPosition = robot.getPosition();

        // Assert
        assertEquals(expectedPosition, actualPosition);
    }

    @Test
    public void whenRoboCleanerIsInitializedWithNegativeCoordinates_thenItsPositionIsSetCorrectly() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(-5, -15, RoboCleaner.Direction.WEST);
        String expectedPosition = "Position: (-5, -15), Facing: WEST";

        // Act
        String actualPosition = robot.getPosition();

        // Assert
        assertEquals(expectedPosition, actualPosition);
    }

    // --- moveForward() Tests ---

    @Test
    public void givenRobotFacingNorth_whenMoveForward_thenYCoordinateIncreases() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.NORTH);
        
        // Act
        robot.moveForward(5);

        // Assert
        assertEquals("Position: (0, 5), Facing: NORTH", robot.getPosition());
    }

    @Test
    public void givenRobotFacingSouth_whenMoveForward_thenYCoordinateDecreases() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(10, 10, RoboCleaner.Direction.SOUTH);

        // Act
        robot.moveForward(3);

        // Assert
        assertEquals("Position: (10, 7), Facing: SOUTH", robot.getPosition());
    }

    @Test
    public void givenRobotFacingEast_whenMoveForward_thenXCoordinateIncreases() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.EAST);

        // Act
        robot.moveForward(7);

        // Assert
        assertEquals("Position: (7, 0), Facing: EAST", robot.getPosition());
    }

    @Test
    public void givenRobotFacingWest_whenMoveForward_thenXCoordinateDecreases() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(5, 5, RoboCleaner.Direction.WEST);

        // Act
        robot.moveForward(4);

        // Assert
        assertEquals("Position: (1, 5), Facing: WEST", robot.getPosition());
    }

    // --- moveForward() Edge Case Tests ---

    @Test
    public void whenMoveForwardWithZeroSteps_thenPositionDoesNotChange() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(2, 3, RoboCleaner.Direction.NORTH);
        String expectedPosition = "Position: (2, 3), Facing: NORTH";

        // Act
        robot.moveForward(0);

        // Assert
        assertEquals(expectedPosition, robot.getPosition());
    }
    
    /**
     * This test characterizes the current behavior for negative steps.
     * The current implementation moves the robot backward.
     */
    @Test
    public void whenMoveForwardWithNegativeSteps_thenMovesBackward() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(10, 10, RoboCleaner.Direction.EAST);

        // Act
        robot.moveForward(-5);

        // Assert
        assertEquals("Position: (5, 10), Facing: EAST", robot.getPosition());
    }


    // --- turnLeft() Tests ---

    @Test
    public void givenRobotFacingNorth_whenTurnLeft_thenFacesWest() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.NORTH);

        // Act
        robot.turnLeft();

        // Assert
        assertEquals("Position: (0, 0), Facing: WEST", robot.getPosition());
    }

    @Test
    public void givenRobotFacingWest_whenTurnLeft_thenFacesSouth() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.WEST);

        // Act
        robot.turnLeft();

        // Assert
        assertEquals("Position: (0, 0), Facing: SOUTH", robot.getPosition());
    }
    
    @Test
    public void whenTurnLeftFourTimes_thenDirectionReturnsToOriginal() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.EAST);
        String expectedPosition = "Position: (0, 0), Facing: EAST";

        // Act
        robot.turnLeft(); // Faces NORTH
        robot.turnLeft(); // Faces WEST
        robot.turnLeft(); // Faces SOUTH
        robot.turnLeft(); // Faces EAST

        // Assert
        assertEquals(expectedPosition, robot.getPosition());
    }

    // --- turnRight() Tests ---

    @Test
    public void givenRobotFacingNorth_whenTurnRight_thenFacesEast() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.NORTH);

        // Act
        robot.turnRight();

        // Assert
        assertEquals("Position: (0, 0), Facing: EAST", robot.getPosition());
    }

    @Test
    public void givenRobotFacingEast_whenTurnRight_thenFacesSouth() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.EAST);

        // Act
        robot.turnRight();

        // Assert
        assertEquals("Position: (0, 0), Facing: SOUTH", robot.getPosition());
    }

    @Test
    public void whenTurnRightFourTimes_thenDirectionReturnsToOriginal() {
        // Arrange
        RoboCleaner robot = new RoboCleaner(0, 0, RoboCleaner.Direction.SOUTH);
        String expectedPosition = "Position: (0, 0), Facing: SOUTH";

        // Act
        robot.turnRight(); // Faces WEST
        robot.turnRight(); // Faces NORTH
        robot.turnRight(); // Faces EAST
        robot.turnRight(); // Faces SOUTH

        // Assert
        assertEquals(expectedPosition, robot.getPosition());
    }

    // --- Complex Sequence Test ---

    @Test
    public void whenExecutingASequenceOfMoves_thenFinalPositionIsCorrect() {
        // Arrange: Start at (1, 2) facing NORTH
        RoboCleaner robot = new RoboCleaner(1, 2, RoboCleaner.Direction.NORTH);

        // Act
        robot.moveForward(3);    // Position: (1, 5), Facing: NORTH
        robot.turnRight();       // Position: (1, 5), Facing: EAST
        robot.moveForward(2);    // Position: (3, 5), Facing: EAST
        robot.turnLeft();        // Position: (3, 5), Facing: NORTH
        robot.turnLeft();        // Position: (3, 5), Facing: WEST
        robot.moveForward(5);    // Position: (-2, 5), Facing: WEST

        // Assert
        assertEquals("Position: (-2, 5), Facing: WEST", robot.getPosition());
    }
}

How to Use These Tests

  1. Setup Your Project: Make sure you have JUnit 4 in your project's dependencies. If you are using Maven, add this to your pom.xml: xml <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>
  2. Place the Files:
    • Place RoboCleaner.java in your main source folder (e.g., src/main/java/com/yourpackage/).
    • Place RoboCleanerTest.java in your test source folder (e.g., src/test/java/com/yourpackage/).
  3. Run the Tests: Run the tests from your IDE or build tool (like Maven or Gradle). All tests should pass, confirming the current behavior.
  4. Refactor with Confidence: Now you can start refactoring your RoboCleaner class. After each significant change, re-run these tests. If any test fails, you'll know immediately that you've altered the existing behavior and can fix it before moving on.

This test suite provides a solid foundation for your refactoring efforts. Happy coding

Model: gemini-2.5-pro
Temperature: 0.2
Max output tokens: 8192
TopK: 40.0
TopP: 0.95