生成 Java 单元测试

为旧版 Java 代码生成单元测试

在对 Cloud 控制台使用不同参数值时,或通过直接调用 Vertex AI API,您可以直接查询模型,并测试返回的结果。

系统指令

您是一位熟练的 Java 开发者,也是测试驱动型开发方面的专家。您的主要目标是编写简洁、高效且可维护的 Java 代码,并确保所有功能都经过了全面测试。

提示Freeform

我是一名 Java 开发者,一直在遵循测试驱动型开发方法。我有以下用于操作机器人的代码,我很快就会重构该代码以添加清洁功能。为了确保在重构过程中不会破坏任何内容,并了解代码的行为,我想为现有代码添加一些特征测试。

  • 请参阅以下代码,并使用 JUnit 4 为 RoboCleaner 类中的方法编写单元测试。
  • 请确保为测试提供有意义的名称,并涵盖所有边缘情况。
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()];
        }
    }
}

响应

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class RoboCleanerTest {

    private RoboCleaner roboCleaner;

    @Before
    public void setUp() {
        roboCleaner = new RoboCleaner(0, 0, RoboCleaner.Direction.NORTH);
    }

    @Test
    public void testInitialPosition() {
        assertEquals("Position: (0, 0), Facing: NORTH", roboCleaner.getPosition());
    }

    @Test
    public void testMoveForwardNorth() {
        roboCleaner.moveForward(5);
        assertEquals("Position: (0, 5), Facing: NORTH", roboCleaner.getPosition());
    }

    @Test
    public void testMoveForwardSouth() {
        roboCleaner.direction = RoboCleaner.Direction.SOUTH;
        roboCleaner.moveForward(3);
        assertEquals("Position: (0, -3), Facing: SOUTH", roboCleaner.getPosition());
    }

    @Test
    public void testMoveForwardEast() {
        roboCleaner.direction = RoboCleaner.Direction.EAST;
        roboCleaner.moveForward(7);
        assertEquals("Position: (7, 0), Facing: EAST", roboCleaner.getPosition());
    }

    @Test
    public void testMoveForwardWest() {
        roboCleaner.direction = RoboCleaner.Direction.WEST;
        roboCleaner.moveForward(2);
        assertEquals("Position: (-2, 0), Facing: WEST", roboCleaner.getPosition());
    }

    @Test
    public void testMoveForwardZeroSteps() {
        roboCleaner.moveForward(0);
        assertEquals("Position: (0, 0), Facing: NORTH", roboCleaner.getPosition());
    }

    @Test
    public void testMoveForwardNegativeSteps() {
        roboCleaner.moveForward(-5); //Should not affect position
        assertEquals("Position: (0, 0), Facing: NORTH", roboCleaner.getPosition());
    }


    @Test
    public void testTurnLeft() {
        roboCleaner.turnLeft();
        assertEquals("Position: (0, 0), Facing: WEST", roboCleaner.getPosition());
    }

    @Test
    public void testTurnLeftFromWest() {
        roboCleaner.direction = RoboCleaner.Direction.WEST;
        roboCleaner.turnLeft();
        assertEquals("Position: (0, 0), Facing: SOUTH", roboCleaner.getPosition());
    }

    @Test
    public void testTurnRight() {
        roboCleaner.turnRight();
        assertEquals("Position: (0, 0), Facing: EAST", roboCleaner.getPosition());
    }

    @Test
    public void testTurnRightFromEast() {
        roboCleaner.direction = RoboCleaner.Direction.EAST;
        roboCleaner.turnRight();
        assertEquals("Position: (0, 0), Facing: SOUTH", roboCleaner.getPosition());
    }

    @Test
    public void testMultipleTurns() {
        roboCleaner.turnLeft();
        roboCleaner.turnRight();
        roboCleaner.turnRight();
        assertEquals("Position: (0, 0), Facing: EAST", roboCleaner.getPosition());
    }

    @Test
    public void testComplexMovement() {
        roboCleaner.moveForward(2);
        roboCleaner.turnRight();
        roboCleaner.moveForward(3);
        roboCleaner.turnLeft();
        roboCleaner.moveForward(1);
        assertEquals("Position: (3, 3), Facing: NORTH", roboCleaner.getPosition());
    }

    @Test
    public void testNegativeCoordinates(){
        roboCleaner = new RoboCleaner(-1,-2, RoboCleaner.Direction.SOUTH);
        roboCleaner.moveForward(1);
        assertEquals("Position: (-1, -3), Facing: SOUTH", roboCleaner.getPosition());
    }
}
型号: gemini-1.5-flash-002
温度: 0.2
输出令牌数量上限: 8192
TopK: 40.0
TopP: 0.95